Symfony2:1到M:在同一Web表单中持久化两个对象


Symfony2: 1 to M: persisting two objects in same webform

当绑定到同一形式的两个不同实体中的持久化数据时,我收到了"国家不应为空"的消息。有什么原因吗?

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

第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();

控制器版本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();

表单类型

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

国家

class Country
{
    protected $id;
    protected $code;
    /**
     * @ORM'OneToMany(targetEntity="League", mappedBy="country", cascade={"persist"})
     */
    protected $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;
}

如上所述,我必须更新我的LeagueType,所以它就是这样开始工作的。我之所以痛苦,是因为我有时会单独使用LeagueType作为一个表单,或者与其他表单类型一起在一个表单中使用,所以当它以组合形式使用时,我现在必须传递一个参数。首选上面的Controller Ver 1。

单独使用时:

new LeagueType();

以组合形式使用时:

new LeagueType(true);

联赛类型

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'))
        ;
    }
}