我在Symfony2中的OneToMany嵌入表單存在持續性問題。Symfony2 OneToMany嵌入表單不保存實體
這一次,實體被保存,但沒有對父類的引用。
我的表看起來像
| id | position | content | about_id |
| 29 | 1 | test 1 | NULL |
我不明白爲什麼這about_id仍然是NULL。
我的關係:
實體公司:
class About {
/*
....
*/
/**
* @ORM\OneToMany(targetEntity="AboutContent", mappedBy="about", cascade={"persist", "remove"})
*/
private $content;
/**
* Add content
*
* @param AboutContent $content
* @return About
*/
public function addContent(AboutContent $content)
{
$this->content[] = $content;
return $this;
}
/**
* Remove content
*
* @param AboutContent $content
*/
public function removeContent(AboutContent $content)
{
$this->content->removeElement($content);
}
/**
* Get content
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getContent()
{
return $this->content;
}
}
我的關於內容:
class AboutContent {
/**
* @ORM\ManyToOne(targetEntity="About", inversedBy="content")
*/
private $about;
/**
* Set about
*
* @param About $about
* @return AboutContent
*/
public function setAbout(About $about = null)
{
$this->about = $about;
return $this;
}
/**
* Get about
*
* @return About
*/
public function getAbout()
{
return $this->about;
}
}
我的控制器已經被我的CRUD自動生成:
/**
* Creates a new About entity.
*
*/
public function createAction(Request $request)
{
$entity = new About();
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('about_show', array('id' => $entity->getId())));
}
return $this->render('AdminBundle:About:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
我形式bu ilder:
$builder
->add('lang', 'choice', array(
'choices' => $this->langs,
'preferred_choices' => array('en'),
)) ->add('media')
->add('content', 'collection', array(
'type' => new AboutContentType(),
'allow_add' => true,
'allow_delete' => true,
'cascade_validation' => true
), array('label'=>'Texts'))
;
}
非常感謝你,如果你找到解決方案。
我不能相信我花了3小時這個... – AlexisWbr 2016-06-03 08:32:40
男人,2年後你已經救了我的一天.... – skytorner 2017-02-21 17:11:10