Symfony2 multiple embedded forms one to many update foreign key

354 views Asked by At

Hello i have a problem with my multiple embedded form (one to many). One game have many prizes and one prize have many options. When i attemp to save this form get an error message

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'fk_prize' cannot be null

I have already set in my entity on cascade and in my form type set by_refference false but didnt work. All the other assigned foreign keys works perfectly.

UPDATED : In controller when i do this form saved successfull. But i want to do this with doctrine. Is it bug in doctrine or something wrong in my code ? Thanks for your time!

            //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

There are 1 answers

0
Karol Wojciechowski On

Doctrine can handle changes in entities only on "owning" side. That means, you can modify relations only in entity where is defined join column/join table. http://doctrine-orm.readthedocs.org/en/latest/reference/unitofwork-associations.html