2014-01-15 82 views
2

使用ZF2和Doctrine 2.尝试使用实体管理器插入数据。学说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(); 

给我错误:上找到关联架构\实体\链接#CATEGORY_ID类型的实体,但预计模式\实体\目录

我尝试也把级联= {“坚持”}在annontations,但给我的错误:类“ '不存在

为什么?

回答

3

你必须在CATEGORY_ID设置为Schema\Entity\Category[]一个值,而不是array()

+0

有点困惑。我怎样才能做到这一点?我只想将value = 1与其他字符串数据一起传递给link实体。 – Nikitas

+0

对于上述问题,我很抱歉。你是对的,我意识到我不知道数据库协会真的在做什么......去学习。谢谢。 – Nikitas