我阅读官方文档和大量的线程,但仍然没有找到我的情况的解决方案。我的情况非常基本。我有两个实体:评论和关键字。一个评论可以有许多关键字,但每个关键字只能用于一个评论。关键字表中的关键字不是唯一的。所以我决定这是一对多的关系。表结构只是想如下:理解学说中关系映射的问题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