0
我得到「國家不應該是空白的。」當綁定在同一形式的兩個不同實體中持久化數據時的消息。任何理由?Symfony2:1到M:堅持兩個對象在相同的網絡表格
RELATION: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();
表格類型
$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;
}
的'country' FIEL d應該在'LeagueType'形式。您應該創建一個'LeagueType'表單類,然後在其中包含'country'字段,以便在插入數據庫時級聯。 – lsouza 2014-09-20 13:25:29
+1。你的提示是解決方案的開始。謝謝。 – BentCoder 2014-09-20 14:04:00