Symfony形式中的多个依赖的一对多关系

时间:2013-09-10 20:15:57

标签: symfony symfony-forms symfony-2.3

我正试图让多关系(多个,依赖的一对多关系)形式起作用,但没有成功。我正在使用Symfony 2.3和FOSUserbundle。

实体用户

    use FOS\UserBundle\Entity\User as BaseUser;
    [...]

    /**
     * @ORM\Entity
     * @Gedmo\Loggable
     * @ORM\Table(name="ta_user", indexes={@ORM\Index(name="IDX_LOGIN_TOKEN", columns={"login_token"})})
     */
    class User extends BaseUser
    {
            [...]

        /**
         * @ORM\OneToMany(targetEntity="UserLifestyle", mappedBy="user", fetch="LAZY", cascade={"persist", "remove"})
         */
        protected $lifestyle;

的UserManager

    use Doctrine\ORM\EntityManager;
    use FOS\UserBundle\Entity\UserManager as BaseUserManager;
    use Acme\UserBundle\Entity\LifestyleQuestion;
    use Acme\UserBundle\Entity\UserLifestyle;
    [...]

    class UserManager extends BaseUserManager {
        public function createUser() {
            $user = parent::createUser();
            $lifestyle = new UserLifestyle();
            $lifestyle->setQuestion($this->objectManager->getReference('Acme\UserBundle\Entity\LifestyleQuestion', 1));
            $user->addLifeStyle($lifestyle);
            $lifestyle = new UserLifestyle();
            $lifestyle->setQuestion($this->objectManager->getReference('Acme\UserBundle\Entity\LifestyleQuestion', 2));
            $user->addLifeStyle($lifestyle);
            $lifestyle = new UserLifestyle();
            $lifestyle->setQuestion($this->objectManager->getReference('Acme\UserBundle\Entity\LifestyleQuestion', 3));
            $user->addLifeStyle($lifestyle);
            return $user;
        }

实体UserLifestyle

    /**
     * @ORM\Entity
     * @Gedmo\Loggable
     * @ORM\Table(name="ta_user_lifestyle")
     */
    class UserLifestyle
    {
        /**
         * @ORM\Id
         * @ORM\Column(type="smallint")
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        protected $id;

        /**
         * @ORM\ManyToOne(targetEntity="User", inversedBy="lifestyle")
         * @ORM\JoinColumn(name="user_id")
         */
        protected $user;

        /**
         * @ORM\ManyToOne(targetEntity="LifestyleQuestion", inversedBy="answeredByUser")
         * @ORM\JoinColumn(name="question_id")
         */
        protected $question;

        /**
         * @ORM\ManyToOne(targetEntity="LifestyleAnswer", inversedBy="userAnswers")
         * @ORM\JoinColumn(name="answer_id")
         * @Gedmo\Versioned
         */
        protected $answer;

然后,有一个表单类型

    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\OptionsResolver\OptionsResolverInterface;
    use Doctrine\ORM\EntityRepository;

    class RegistrationType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('email', NULL, array('label' => 'E-Mail'))
                            [...]
                ->add('lifestyle', 'collection', array(
                    'type' => new RegistrationLifestyleType(),
                    'allow_add' => false,
                    'allow_delete' => false,
                    'label' => false,
                ))

现在应该有一个相关的RegistrationLifestyleType。但我不知道它应该是什么样子。我希望,我的注册表单中有三个选项字段,显示与这些问题相关的问题(作为标签)和一堆答案(作为选择字段)。 UserManager为新创建的用户分配了三个问题,因此可以通过以下方式获得问题:

    $lifestyles = $user->getLifestyles();
    foreach ($lifestyles as $lifestyle) {
        $question = $lifestyle->getQuestion(); // echo $question->getQuestion();
        $answers = $lifestyle->getQuestion()->getAnswers(); // loop through $answers and echo $answer->getAnswer();
    }

但我如何修改表单类型,以使其工作。重要提示:我的目的是尽可能使用内置功能,并尝试通过注入服务容器和实体管理器来避免使表单类型和其他类型膨胀。

1 个答案:

答案 0 :(得分:1)

找到解决方案,也许有人可以使用它。问题似乎是LifestyleQuestionLifestyleAnswer在同一个对象(UserLifestyle)上是1:n关系,所以Symfony不知道如何处理它,即使我设置了{ {1}}已LifestyleQuestion中的特定问题。关于https://stackoverflow.com/a/9729888/672452,必须使用表单侦听器,因此父对象以子表单形式提供。所以这是我的“简单”UserManager(不使用任何注入的容器或管理器):

RegistrationLifestyleType