symfony2嵌入式格式赢得';不能持久化到数据库
为此,我遵循了文档:但无法将示例持久化到嵌入表单的数据库中;植物班保存得很好。我假设控制器中的persist和flush方法处理这两个实体的持久化。这是错误的假设吗?在刷新之前,我是否需要拦截它并在控制器中手动设置它 无论如何,这是我的代码:symfony2嵌入式格式赢得';不能持久化到数据库,symfony,doctrine-orm,Symfony,Doctrine Orm,为此,我遵循了文档:但无法将示例持久化到嵌入表单的数据库中;植物班保存得很好。我假设控制器中的persist和flush方法处理这两个实体的持久化。这是错误的假设吗?在刷新之前,我是否需要拦截它并在控制器中手动设置它 无论如何,这是我的代码: Plant Entity: <?php /** * @ORM\Entity(repositoryClass="Blogger\BlogBundle\Entity\Repository\PlantRepository") * @ORM\Tabl
Plant Entity:
<?php
/**
* @ORM\Entity(repositoryClass="Blogger\BlogBundle\Entity\Repository\PlantRepository")
* @ORM\Table(name="plant")
* @ORM\HasLifecycleCallbacks
*/
class Plant
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="array", nullable=true)
* @ORM\ManyToMany(targetEntity="Blogger\BlogBundle\Entity\Picture", inversedBy="plants", cascade={"persist"})
* @ORM\JoinTable(name="picture")
*/
protected $pictures;
//...
public function __construct()
{
$this->pictures = new ArrayCollection;
}
//...
/**
* Add pictures
*
* @param \Blogger\BlogBundle\Entity\Picture $pictures
* @return Plant
*/
public function addPicture(\Blogger\BlogBundle\Entity\Picture $pictures)
{
$pictures->addPlant($this);
$this->pictures[] = $pictures;
}
/**
* Remove pictures
*
* @param \Blogger\BlogBundle\Entity\Picture $pictures
*/
public function removePicture(\Blogger\BlogBundle\Entity\Picture $pictures)
{
$this->pictures->removeElement($pictures);
}
/**
* Get pictures
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getPictures()
{
return $this->pictures;
}
}
我怀疑我没有正确映射数据库的注释。当我打开phpadmin时,数据库中没有定义任何关系。我找到了它。这确实是我的注释中的一个错误。这个问题实际上在一篇帖子中得到了回答。对于我的解决方案,它看起来是这样的: 工厂实体:
class Plant
{
//..
/**
* @ORM\ManyToMany(targetEntity="Blogger\BlogBundle\Entity\Picture", inversedBy="plants", cascade={"persist"})
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
*/
protected $pictures;
/**
* Constructor
*/
public function __construct()
{
$this->pictures = new ArrayCollection();
}
public function addPicture(\Blogger\BlogBundle\Entity\Picture $pictures)
{
$pictures->addPlant($this);
$this->pictures[] = $pictures;
}
//..
}
class Picture
{
//..
/**
* @ORM\ManyToMany(targetEntity="Blogger\BlogBundle\Entity\Plant", mappedBy="pictures", cascade={"persist", "remove"})
*/
private $plants;
/**
* Constructor
*/
public function __construct()
{
$this->plants = new \Doctrine\Common\Collections\ArrayCollection();
}
public function addPlant(\Blogger\BlogBundle\Entity\Plant $plants)
{
if (!$this->plants->contains($plants)) {
$this->plants->add($plants);
}
}
//..
}
控制器:
public function newAction(Request $request){
$plant = new Plant();
$image1 = new Picture();
$plant->getPictures()->add($image1);
$form = $this->createForm(new PlantForm($this->getDoctrine()->getManager()), $plant);
if ($request->getMethod() == 'POST') {
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()
->getEntityManager();
$em->persist($plant);
$em->flush();
return $this->redirect($this->generateUrl('route', array(
'id' => $plant->getId()
)));
}
}
return $this->render('Bundle:Plant:new.html.twig', array(
'form' => $form->createView()
));
}
公共函数newAction(请求$Request){
也许对其他人来说是一个有用的提示:这个映射创建了一个名为plant_picture的新表,它保存了这个关联列根本没有添加到Plant实体中。我找到了它。这确实是我注释中的一个错误。这个问题实际上在一篇文章中得到了回答。对于我的解决方案,它看起来是这样的: 工厂实体:
class Plant
{
//..
/**
* @ORM\ManyToMany(targetEntity="Blogger\BlogBundle\Entity\Picture", inversedBy="plants", cascade={"persist"})
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
*/
protected $pictures;
/**
* Constructor
*/
public function __construct()
{
$this->pictures = new ArrayCollection();
}
public function addPicture(\Blogger\BlogBundle\Entity\Picture $pictures)
{
$pictures->addPlant($this);
$this->pictures[] = $pictures;
}
//..
}
class Picture
{
//..
/**
* @ORM\ManyToMany(targetEntity="Blogger\BlogBundle\Entity\Plant", mappedBy="pictures", cascade={"persist", "remove"})
*/
private $plants;
/**
* Constructor
*/
public function __construct()
{
$this->plants = new \Doctrine\Common\Collections\ArrayCollection();
}
public function addPlant(\Blogger\BlogBundle\Entity\Plant $plants)
{
if (!$this->plants->contains($plants)) {
$this->plants->add($plants);
}
}
//..
}
控制器:
public function newAction(Request $request){
$plant = new Plant();
$image1 = new Picture();
$plant->getPictures()->add($image1);
$form = $this->createForm(new PlantForm($this->getDoctrine()->getManager()), $plant);
if ($request->getMethod() == 'POST') {
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()
->getEntityManager();
$em->persist($plant);
$em->flush();
return $this->redirect($this->generateUrl('route', array(
'id' => $plant->getId()
)));
}
}
return $this->render('Bundle:Plant:new.html.twig', array(
'form' => $form->createView()
));
}
公共函数newAction(请求$Request){
对其他人来说,这可能是一个有用的提示:此映射创建了一个名为plant_picture的新表,该表保存了关联。我惊讶地发现一个“picture”列根本没有添加到plant实体中。在持久化的plant之后添加此代码并对其进行测试:
$em->persist($image1)
,也许它确实有效,但我忘了取出$image1虚拟数据(它本来不应该在那里)。我已经这样做了,现在当图片被动态添加到表单中时,它不会持久化图片。plant中的图片字段实际上是持久化整个plant对象…包括动态添加的图片。这很有意义。它被映射为数组。这些字段应该映射为什么?保留为空提供了一个错误。在持久化工厂之后添加此代码并对其进行测试:$em->persist($image1)
,可能它确实有效,但我忽略了取出$image1虚拟数据(它本来不应该在那里)。我已经这样做了,现在当图片被动态添加到表单中时,它不会持久化图片。plant中的图片字段实际上是持久化整个plant对象…包括动态添加的图片。这很有意义。它被映射为数组。这些字段应该映射为什么?保留为空提供了一个错误。
class Plant
{
//..
/**
* @ORM\ManyToMany(targetEntity="Blogger\BlogBundle\Entity\Picture", inversedBy="plants", cascade={"persist"})
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
*/
protected $pictures;
/**
* Constructor
*/
public function __construct()
{
$this->pictures = new ArrayCollection();
}
public function addPicture(\Blogger\BlogBundle\Entity\Picture $pictures)
{
$pictures->addPlant($this);
$this->pictures[] = $pictures;
}
//..
}
class Picture
{
//..
/**
* @ORM\ManyToMany(targetEntity="Blogger\BlogBundle\Entity\Plant", mappedBy="pictures", cascade={"persist", "remove"})
*/
private $plants;
/**
* Constructor
*/
public function __construct()
{
$this->plants = new \Doctrine\Common\Collections\ArrayCollection();
}
public function addPlant(\Blogger\BlogBundle\Entity\Plant $plants)
{
if (!$this->plants->contains($plants)) {
$this->plants->add($plants);
}
}
//..
}
$plant = new Plant();
if ($request->getMethod() == 'POST') {
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()
->getEntityManager();
$em->persist($plant);
$em->flush();
return $this->redirect($this->generateUrl('BloggerBlogBundle_plant_library_show', array(
'id' => $plant->getId()
)));
}
}
return $this->render('BloggerBlogBundle:Library:new.html.twig', array(
'form' => $form->createView()
));
}