Symfony2多个嵌入式表单一对多更新外键

时间:2014-11-11 23:12:01

标签: php forms symfony-2.5

您好我的多重嵌入表单(一对多)有问题。 一个游戏有很多奖项,一个奖项有很多选项。当我尝试保存此表单时收到错误消息

SQLSTATE [23000]:违反完整性约束:1048列' fk_prize'不能为空

我已经在cascade上设置了我的实体,并且在我的表单类型中设置了by_refference false但是没有用。所有其他指定的外键都可以正常工作。

更新: 在控制器中,当我这样做时,表单保存成功。但我想用学说来做这件事。它是学说中的错误还是我的代码中的错误? 谢谢你的时间!

            //Hacked code in controller to save the form
            $prizes = $data->getPrizes();
            foreach ($prizes as $prize) {
                $prizeOptions = $prize->getPrizesOptions();
                foreach ($prizeOptions as $prizeOption) {
                    $prizeOption->setPrize($prize);
                }
            }
            $em->persist($data);
            $em->flush();





<?php 
    class Game
    {
        /**
         * @ORM\OneToMany(targetEntity="Prize", mappedBy="game", cascade={"persist"})
         */
        protected $prizes;

        public function __construct()
        {
            $this->gameUsers = new ArrayCollection();
            $this->prizes = new ArrayCollection();
        }


    }
    ?>      

<?php           

        class GameType extends AbstractType
        {

            /**
             * @param FormBuilderInterface $builder
             * @param array $options
             */
            public function buildForm(FormBuilderInterface $builder, array $options)
            {


                $builder
                    ->add('alias', 'text' , [
                        'label'=>'Name'
                    ])               
                    ->add('prizes' ,'collection', array(
                        'type'         => new PrizeType($this->intention),
                        'allow_add'    => true,
                        'allow_delete' => false,
                        'prototype' => true,
                        'by_reference' => false,
                        'label' => false,
                    ))                
                    ->add('save', 'submit', [
                        'attr'   =>  [
                            'class'   => 'btn btn-primary'
                        ]
                    ]);
            }                
        }

    <?php

    class Prize 
    {

    /**
     * The Game
     * @ORM\ManyToOne(targetEntity="Game")
     * @ORM\JoinColumn(name="game_id", referencedColumnName="id")
     */
    protected $game;

    /**
     * @ORM\OneToMany(targetEntity="PrizeOptions", mappedBy="prize", cascade={"persist"})
     */
    protected $prizes_options;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->prizes_options = new \Doctrine\Common\Collections\ArrayCollection();
    }
    }

    class PrizeType extends AbstractType
    {

        /**
         * @param FormBuilderInterface $builder
         * @param array $options
         */
        public function buildForm(FormBuilderInterface $builder, array $options)
        {

            $builder            
                ->add('prizes_options' ,'collection', array(
                    'type' => new PrizeOptionsType(),
                    'allow_add'    => true,
                    'allow_delete' => true,
                    'by_reference' => false,
                    'label' => false,
                ))    
            ;
        }


    }

    <?php

    class PrizeOptionsType extends AbstractType
    {

        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('name', 'text' , [
                    'label'=>'Value'
                ])
                ;
        }

    }

1 个答案:

答案 0 :(得分:0)

Doctrine只能在“拥有”方面处理实体的变化。这意味着,您只能在已定义连接列/连接表的实体中修改关系。 http://doctrine-orm.readthedocs.org/en/latest/reference/unitofwork-associations.html