使用ZF2和Doctrine 2.尝试使用实体管理器插入数据。
我有这个实体:
class Link
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
protected $link_id;
/** @ORM\Column(type="string", length=255, nullable=false) */
protected $title;
/** @ORM\Column(type="string", length=255, nullable=false) */
protected $short_description;
/** @ORM\Column(columnDefinition="LONGTEXT NOT NULL") */
protected $description;
/** @ORM\Column(type="string", length=255, nullable=false) */
protected $webpage_url;
/** @ORM\Column(type="string", length=255, nullable=false) */
protected $email;
/** @ORM\Column(type="string", length=255, nullable=false) */
protected $meta_keys;
/** @ORM\Column(type="datetime", columnDefinition="DATETIME NOT NULL") */
protected $date_created;
/**
* @ORM\ManyToOne(targetEntity="Schema\Entity\Category")
**/
private $category_id;
public function __get($property) {
if (property_exists($this, $property)) {
return $this->$property;
}
}
public function __set($property, $value) {
if (property_exists($this, $property)) {
$this->$property = $value;
}
return $this;
}
}
这个
class LinkType
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
protected $link_type_id;
/** @ORM\Column(type="string", length=255, nullable=false) */
protected $name;
public function __get($property) {
if (property_exists($this, $property)) {
return $this->$property;
}
}
public function __set($property, $value) {
if (property_exists($this, $property)) {
$this->$property = $value;
}
return $this;
}
}
当我尝试这个时:
$link = new Link();
$link->title = 'aa';
$link->category_id = array('1');
$link->link_type_id = array('1');
$link->description = 'adsfa';
$link->webpage_url = 'asdfad';
$link->short_description = 'aa';
$link->email = 'asdf';
$link->meta_keys = 'asdf';
$link->date_created ='2014-01-14 13:26:54';
$this->getObjectManager()->persist($link); // ?????
$this->getObjectManager()->flush();
给我错误:在关联Schema \ Entity \ Link#category_id上找到类型的实体,但期望Schema \ Entity \ Category
我还尝试将cascade = {“persist”}放入公告中,但却给出了错误: Class''不存在
为什么?
答案 0 :(得分:3)
您必须将category_id设置为Schema\Entity\Category[]
而不是array()