我是Symfony2的新手(与Symfony 1合作多年),我试图在一个与另一个实体有关系的实体中插入一些记录,这里是:
<?php
namespace Jjj\SomeBundle\Entity;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Translatable\Translatable;
/**
* Jjj\SomeBundle\Entity
*
* @ORM\Table(name="content")
* @ORM\Entity(repositoryClass="Jjj\SomeBundle\Entity\ContentRepository")
*/
class Content implements Translatable
{
/**
*
* @ORM\Column(name="id", type="bigint", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="contents")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
protected $category_id;
/**
* @Gedmo\Translatable
* @ORM\Column(name="title", type="string", length=32, nullable=false)
*/
protected $title;
/**
* @Gedmo\Translatable
* @ORM\Column(name="summary", type="text", nullable=true)
*/
protected $summary;
/**
* @Gedmo\Translatable
* @ORM\Column(name="fulltext", type="text", nullable=true)
*/
protected $fulltext;
/**
*
* @ORM\Column(name="created_at", type="datetime", nullable=true)
*/
protected $created_at;
/**
*
* @ORM\Column(name="updated_at", type="datetime", nullable=true)
*/
protected $updated_at;
/**
* @Gedmo\Slug(fields={"title"})
* @ORM\Column(length=128, unique=true)
*/
private $slug;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set category_id
*
* @param integer $categoryId
* @return Content
*/
public function setCategoryId($categoryId)
{
$this->category_id = $categoryId;
return $this;
}
/**
* Get category_id
*
* @return integer
*/
public function getCategoryId()
{
return $this->category_id;
}
/**
* Set title
*
* @param string $title
* @return Content
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set slug
*
* @param string $slug
* @return Content
*/
public function setSlug($slug)
{
$this->slug = $slug;
return $this;
}
/**
* Get slug
*
* @return string
*/
public function getSlug()
{
return $this->slug;
}
/**
* Set summary
*
* @param string $summary
* @return Content
*/
public function setSummary($summary)
{
$this->summary = $summary;
return $this;
}
/**
* Get summary
*
* @return string
*/
public function getSummary()
{
return $this->summary;
}
/**
* Set fulltext
*
* @param string $fulltext
* @return Content
*/
public function setFulltext($fulltext)
{
$this->fulltext = $fulltext;
return $this;
}
/**
* Get fulltext
*
* @return string
*/
public function getFulltext()
{
return $this->fulltext;
}
/**
* Set created_at
*
* @param \DateTime $createdAt
* @return Content
*/
public function setCreatedAt($createdAt)
{
$this->created_at = $createdAt;
return $this;
}
/**
* Get created_at
*
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->created_at;
}
/**
* Set updated_at
*
* @param \DateTime $updatedAt
* @return Content
*/
public function setUpdatedAt($updatedAt)
{
$this->updated_at = $updatedAt;
return $this;
}
/**
* Get updated_at
*
* @return \DateTime
*/
public function getUpdatedAt()
{
return $this->updated_at;
}
}
这是类别实体:
<?php
namespace Jjj\SomeBundle\Entity;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
/**
* Jjj\SomeBundle\Entity
*
* @ORM\Table(name="category")
* @ORM\Entity(repositoryClass="Jjj\SomeBundle\Entity\CategoryRepository")
*/
class Category
{
/**
* @var integer
*
* @ORM\Column(name="id", type="bigint", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=32, nullable=false)
*/
protected $title;
/**
* @var string
*
* @ORM\Column(name="description", type="string", length=255, nullable=false)
*/
protected $description;
/**
* @var string
*
* @ORM\Column(name="created_at", type="datetime", nullable=false)
*/
protected $created_at;
/**
* @var string
*
* @ORM\Column(name="updated_at", type="datetime", nullable=false)
*/
protected $updated_at;
/**
* @Gedmo\Slug(fields={"title"})
* @ORM\Column(length=128, unique=true)
*/
private $slug;
/**
* @ORM\OneToMany(targetEntity="Content", mappedBy="category")
*/
protected $contents;
public function __construct()
{
$this->contents = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
* @return Category
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set description
*
* @param string $description
* @return Category
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set created_at
*
* @param \DateTime $createdAt
* @return Category
*/
public function setCreatedAt($createdAt)
{
$this->created_at = $createdAt;
return $this;
}
/**
* Get created_at
*
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->created_at;
}
/**
* Set updated_at
*
* @param \DateTime $updatedAt
* @return Category
*/
public function setUpdatedAt($updatedAt)
{
$this->updated_at = $updatedAt;
return $this;
}
/**
* Get updated_at
*
* @return \DateTime
*/
public function getUpdatedAt()
{
return $this->updated_at;
}
/**
* Set slug
*
* @param string $slug
* @return Category
*/
public function setSlug($slug)
{
$this->slug = $slug;
return $this;
}
/**
* Get slug
*
* @return string
*/
public function getSlug()
{
return $this->slug;
}
/**
* Add contents
*
* @param \Jaguar\AloBundle\Entity\Content $contents
* @return Category
*/
public function addContent(\Jaguar\AloBundle\Entity\Content $contents)
{
$this->contents[] = $contents;
return $this;
}
/**
* Remove contents
*
* @param \Jjj\SomeBundle\Entity\Content $contents
*/
public function removeContent(\Jjj\SomeBundle\Entity\Content $contents)
{
$this->contents->removeElement($contents);
}
/**
* Get contents
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getContents()
{
return $this->contents;
}
}
在正在运行的控制器中,我写了这个:
$content = new Content();
$content->setTitle('Content Example');
$content->setSummary('Content Example');
$content->setFulltext('My first content...');
$content->setCategoryId(2);
$em = $this->getDoctrine()->getEntityManager();
$em->persist($content);
$em->flush();
错误说:
ContextErrorException:警告:spl_object_hash()期望参数1为对象,整数在D:\ xampp \ htdocs \ projects \ alopatria \ vendor \ doctrine \ orm \ lib \ Doctrine \ ORM \ UnitOfWork.php第1367行中给出
我谷歌搜索了一段时间但没有找到运气。
请帮忙吗?
谢谢!
答案 0 :(得分:1)
您正在以错误的方式在Content.php
中进行映射。
如果您要为Category
实体设置ManyToOne关系,则应该有$category
而不是$category_id
属性。你应该处理对象,而不是整数。
因此,您的Content
实体应如下所示:
<?php
// Jjj\SomeBundle\Entity\Content.php
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="contents")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
protected $category;
/**
* Set category
*
* @param \Jjj\SomeBundle\Entity\Category $category
* @return Content
*/
public function setCategory($category)
{
$this->category = $category;
return $this;
}
/**
* Get category
*
* @return category
*/
public function getCategory()
{
return $this->category;
}
你的控制器是这样的:
<?php
$category = new Category();
//… $category->setTitle() and so on…
$content = new Content();
$content->setTitle('Content Example');
$content->setSummary('Content Example');
$content->setFulltext('My first content...');
$content->setCategory($category);
$em = $this->getDoctrine()->getEntityManager();
$em->persist($category);
$em->persist($content);
$em->flush();
然后,您将能够访问Category
实体(已从数据库中提取)的ID:
$categoryId = $category->getId();