Symfony2:1到M:在同一个webform中持久保存两个对象

时间:2014-09-20 11:57:16

标签: php symfony doctrine-orm

我得到"国家不应该是空白。"捆绑以相同形式在两个不同实体中保存数据的消息。有什么理由吗?

关系Country (1) -> (M) League

CONTROLLER VER 1

        $submission = $form->getData();
        $countryData = $submission['country'];
        $leagueData = $submission['league'];
        $em = $this->getDoctrine()->getManager();

        $country = new Country();
        $country->setCode($countryData->getCode());
        $em->persist($country);

        $league = new League();
        $league->setName($leagueData->getName());
        $league->setCountry($country);
        $em->persist($league);

        $em->flush();

CONTROLLER VER 2

        $submission = $form->getData();
        $countryData = $submission['country'];
        $leagueData = $submission['league'];
        $em = $this->getDoctrine()->getManager();

        $country = new Country();
        $country->setCode($countryData->getCode());

        $league = new League();
        $league->setName($leagueData->getName());
        $league->setCountry($country);

        $em->persist($country);
        $em->persist($league);
        $em->flush();

FORM TYPE

    $builder
        ->setMethod('POST')
        ->setAction($options['action'])
        ->add('country', new CountryType())
        ->add('league', new LeagueType())
        ->add('button', 'submit', array('label' => 'Submit'))
        ;

COUNTRY

class Country
{
    protected $id;
    protected $code;

    /**
     * @ORM\OneToMany(targetEntity="League", mappedBy="country", cascade={"persist"})
     */
    protected $league;
}

LEAGUE

class League
{
    protected $id;
    protected $name;

    /**
     * @ORM\ManyToOne(targetEntity="Country", inversedBy="league", cascade={"persist"})
     * @ORM\JoinColumn(name="country_id", referencedColumnName="id", nullable=false)
     * @Assert\NotBlank(message="The Country should not be blank.")
     */
    protected $country;
}

1 个答案:

答案 0 :(得分:1)

正如上面描述的@Isouza,我不得不更新我的LeagueType,所以这就是它开始工作的方式。我之所以遭遇是因为我有时会将LeagueType作为一个表单使用,或者在一个表单中使用其他表单类型,所以我现在必须在组合表单中使用时传递参数。上面的控制器版本1是首选。

单独使用时:

new LeagueType();

以组合形式使用时:

new LeagueType(true);

<强> LeagueType

class LeagueType extends AbstractType
{
    private $cascadeCountry;

    public function __construct($cascadeCountry = false)
    {
        $this->cascadeCountry = $cascadeCountry;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->setAction($options['action'])
            ->setMethod('POST')
            ->add('name', 'text', array('label' => 'Name'))
        ;

        //Used when forms are combined
        if ($this->cascadeCountry === true) {
            $builder
                ->add('country', new CountryType())
            ;
        //Used when the form is used on it own
        } else {
            $builder
                ->add('country', 'entity', array(
                        'label' =>'Country',
                        'class' => 'FootballTeamBundle:Country',
                        'property' => 'name',
                        'multiple' => false,
                        'expanded' => false,
                        'empty_value' => '',
                        'query_builder' =>
                            function (EntityRepository $repo) {
                                return $repo->createQueryBuilder('c')->orderBy('c.name', 'ASC');
                            }
                    ))
            ;
        }

        $builder
            ->add('button', 'submit', array('label' => 'Submit'))
        ;
    }
}