我试图在symfony上创建一个包含子表单的简单表单。我的问题是,当我坚持并反对控制器时
$em->persist($professional);
foreach($form['degrees'] as $key){
$degree = new ProfessionalDegree();
$degree->setTitle($key['title']->getData());
$degree->setPlace($key['place']->getData());
$degree->setDateIn(new \DateTime($key['date_in']->getData()->format('Y-m-d')));
$degree->setDateOut(new \DateTime($key['date_out']->getData()->format('Y-m-d')));
$degree->setProfessional($professional);
$em->persist($degree);
}
$em->flush()
数据库确实添加了两个字段,一个是专业版为NULL,另一个是良好关系:
// UPDATED //
Here is the ProfessionalExperience entity
/**
* @ORM\Entity
* @ORM\Table(name="professional_experience")
*/
class ProfessionalExperience
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
public function __construct()
{
}
/**
* @ORM\Column(type="string")
*
*/
private $company;
/**
* @ORM\Column(type="string")
*
*/
private $position;
/**
* @ORM\Column(type="date")
*
*/
private $date_in;
/**
* @ORM\Column(type="date")
*
*/
private $date_out;
/**
* @ORM\Column(type="string", nullable=true)
*
*/
private $country;
/**
* @ORM\Column(type="string", nullable=true)
*
*/
private $city;
/**
* @ORM\Column(type="text")
*
*/
private $description;
/**
* @ORM\ManyToOne(targetEntity="Professional", inversedBy="experiences", cascade={"persist"})
* @ORM\JoinColumn(name="professional_id", referencedColumnName="id")
*/
private $professional;
...
这是我的专业实体
class Professional extends User
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
public function __construct()
{
$this->followers = new \Doctrine\Common\Collections\ArrayCollection();
$this->following = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* @ORM\Column(type="string", length=255, nullable=false)
*/
private $name;
...
/**
* @ORM\OneToMany(targetEntity="ProfessionalDegree", mappedBy="professional", cascade={"all"})
*/
private $degrees;
// EDITED 2 //
有我的表格:
protected function editProfileFormProfessional($professional, $request) {
$form = $this->createForm(ProfessionalType::class, $professional, [
...
$form->add('degrees', CollectionType::class, array(
'entry_type' => DegreeType::class,
'allow_add' => true,
'allow_delete' => true,
));
if ($form->isSubmitted()) {
if($form->isValid()){
$em = $this->getDoctrine()->getManager();
$professional->setName($form['name']->getData());
$professional->setSurName($form['surname']->getData());
$professional->setProfession($form['profession']->getData());
$professional->setCountry($form['country']->getData());
$professional->setProvince($form['province']->getData());
$professional->setCity($form['city']->getData());
$professional->setZipCode($form['zip_code']->getData());
$professional->setBirthPlace($form['birth_place']->getData());
$professional->setRefereeNumber($form['referee_number']->getData());
$professional->setRefereePlace($form['referee_place']->getData());
$professional->setAboutMe($form['about_me']->getData());
$em->persist($professional);
$em->flush();
foreach($form['degrees'] as $key){
$degree = new ProfessionalDegree();
$degree->setTitle($key['title']->getData());
$degree->setPlace($key['place']->getData());
$degree->setDateIn(new \DateTime($key['date_in']->getData()->format('Y-m-d')));
$degree->setDateOut(new \DateTime($key['date_out']->getData()->format('Y-m-d')));
$degree->setProfessional($professional);
$em->persist($degree);
}
$em->flush();
请注意,PorfesionalDegree已全部级联,因为如果我不添加这些,我会有持续级联错误。
// EDITED 3
现在我知道错误在哪里,在所有集合之前调用句柄请求时,它会自己添加字段。 但问题是专业人士仍然是NULL,我通过添加以下内容来修改我的表单:
->add('degrees', CollectionType::class, array(
'entry_type' => DegreeType::class,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
));
使用by_reference为false,它应该只是这样做,但仍然将专业设置为NULL。我已经按照Symfony在其指南中所说的那样:
但仍然没有
有谁知道为什么? 谢谢
答案 0 :(得分:0)
我认为你不需要在foreach循环之前刷新。
要正确调试,您可以观察循环中发生的事情:
foreach($form['degrees'] as $key){
$degree = new ProfessionalDegree();
$degree->setTitle($key['title']->getData());
$degree->setPlace($key['place']->getData());
$degree->setDateIn(new \DateTime($key['date_in']->getData()->format('Y-m-d')));
$degree->setDateOut(new \DateTime($key['date_out']->getData()->format('Y-m-d')));
$degree->setProfessional($professional);
var_dump($degree->getId());
var_dump($degree->getProfessional());
$em->persist($degree);
}
die();
在这里,您将更好地了解发生的事情并根据需要进行调试。
请不要像这样设置你的$ professional对象。使用:
$form->handleRequest($request);
最后使用FormType:http://symfony.com/doc/current/forms.html#creating-form-classes
祝你好运:)