1

您好,我的多个嵌入表单(一对多)有问题。一场比赛有多种奖品,一种奖品有多种选择。当我尝试保存此表单时收到错误消息

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

我已经在我的实体中设置了级联并且在我的表单类型中设置了 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'
                ])
                ;
        }

    }
4

1 回答 1

0

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

于 2014-11-15T07:45:21.633 回答