我閱讀官方文檔和大量的線程,但仍然沒有找到我的情況的解決方案。我的情況非常基本。我有兩個實體:評論和關鍵字。一個評論可以有許多關鍵字,但每個關鍵字只能用於一個評論。關鍵字表中的關鍵字不是唯一的。所以我決定這是一對多的關係。表結構只是想如下:理解學說中關係映射的問題2
關鍵字
id int(11)
comment_id int(11)
text varchar(30)
評論
id int(11)
text text
這裏是我如何映射他們:
/**
* @Entity
* @Table(name="comments")
**/
class Comments
{
/** @Id @Column(type="integer") */
private $id;
/** @Column(type="text") */
private $text;
/**
* @OneToMany(targetEntity="keywords", mappedBy="comment_id")
*/
private $keywords;
public function getText(){return $this->text;}
public function getId(){return $this->id;}
public function getKeywords(){return $this->keywords;}
}
/**
* @Entity
* @Table(name="keywords")
*/
class Keywords
{
/** @Id @Column(type="integer") */
private $id;
private $text;
public function getText(){return $this->text;}
public function getId(){return $this->id;}
}
,以及如何使用它是這樣的:
$comments = $this->em->getRepository('comments')->findAll();
foreach($comments as $comment){
foreach($comment->getKeywords() as $keyword){
$keyword->getText();
}
}
,並得到了這個錯誤:
Notice: Undefined index: comment_id in C:\web_includes\doctrine\ORM\Persisters\BasicEntityPersister.php on line 1096
Notice: Trying to get property of non-object in C:\web_includes\doctrine\ORM\Persisters\BasicEntityPersister.php on line 1098
Warning: Invalid argument supplied for foreach() in C:\web_includes\doctrine\ORM\Persisters\BasicEntityPersister.php on line 1098
Notice: Undefined index: comment_id in C:\web_includes\doctrine\ORM\PersistentCollection.php on line 168
Fatal error: Call to a member function setValue() on a non-object in C:\web_includes\doctrine\ORM\PersistentCollection.php on line 169
有什麼不對?應該在哪裏定義comment_id?我的映射是否正確?我真的堅持,需要幫助,所以請任何意見非常歡迎。
怎麼樣'comment_id'?你如何設置關係betn評論和關鍵字? – Sadat 2010-07-25 10:38:59
comment_id是關鍵字表中數據庫中具有此關鍵字所屬註釋ID的字段。我想教條會在選擇關鍵字時使用它。 關係是這樣的 OneToMany(targetEntity =「keywords」,mappedBy =「comment_id」) 是不是正確? – SET 2010-07-25 11:03:22