2014-09-20 27 views
0

我得到「國家不應該是空白的。」當綁定在同一形式的兩個不同實體中持久化數據時的消息。任何理由?Symfony2:1到M:堅持兩個對象在相同的網絡表格

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

表格類型

$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

的'country' FIEL d應該在'LeagueType'形式。您應該創建一個'LeagueType'表單類,然後在其中包含'country'字段,以便在插入數據庫時​​級聯。 – lsouza 2014-09-20 13:25:29

+0

+1。你的提示是解決方案的開始。謝謝。 – BentCoder 2014-09-20 14:04:00

回答

1

如上所述@Isouza,我不得不更新我的LeagueType所以這是怎麼開始工作。我之所以受苦是因爲我有時單獨使用LeagueType作爲表單或與其他formtypes一起使用,所以我現在必須傳遞參數以用於組合形式。上面的控制器版本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')) 
     ; 
    } 
}