Php 条令2中关系映射的理解问题
我阅读了官方文档和大量的线程,但仍然没有找到解决我的问题的方法。我的情况很基本。我有两个实体:注释和关键字。一条评论可以有多个关键字,但每个关键字只能用于一条评论。关键字在关键字表中不是唯一的。所以我决定这是一对多的关系。表结构如下所示: 关键词 评论 下面是我如何映射它们的:Php 条令2中关系映射的理解问题,php,doctrine,Php,Doctrine,我阅读了官方文档和大量的线程,但仍然没有找到解决我的问题的方法。我的情况很基本。我有两个实体:注释和关键字。一条评论可以有多个关键字,但每个关键字只能用于一条评论。关键字在关键字表中不是唯一的。所以我决定这是一对多的关系。表结构如下所示: 关键词 评论 下面是我如何映射它们的: /** * @Entity * @Table(name="comments") **/ class Comments { /** @Id @Column(type="integer") */
/**
* @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();
}
}
得到了以下错误:
注意:未定义的索引:C:\web\u includes\doctrine\ORM\Persisters\basicentypersister.php中的注释\u id,位于第1096行
注意:尝试获取C:\web\u includes\doctrine\ORM\Persisters\basicnitypersister.php中非对象的属性(第1098行)
警告:为C:\web\u includes\doctrine\ORM\Persisters\basicnitypersister.php中的foreach()提供的参数无效,位于第1098行
注意:未定义的索引:第168行C:\web\u includes\doctrine\ORM\PersistentCollection.php中的注释\u id
致命错误:在C:\web\u includes\doctor\ORM\PersistentCollection.php第169行的非对象上调用成员函数setValue()
怎么了?应该在哪里定义注释id?我的映射正确吗?我真的被卡住了,需要帮助,所以请提供任何建议。mappedBy属性没有说明外键的名称,这就是“@JoinColumn”注释的用途。此场景的正确映射为:
/**
* @Entity
* @Table(name="comments")
**/
class Comments
{
/** @Id @Column(type="integer") */
private $id;
/** @Column(type="text") */
private $text;
/**
* @OneToMany(targetEntity="keywords", mappedBy="comment")
*/
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;
/**
* @ManyToOne(targetEntity="Comments", inversedBy="keywords")
*/
private $comment;
/**
* @Column(type="text") */
private $text;
public function getText(){return $this->text;}
public function getId(){return $this->id;}
}
它使用模式工具生成与您的模式相等的SQL:
CREATE TABLE comments (id INT NOT NULL, text LONGTEXT NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB;
CREATE TABLE keywords (id INT NOT NULL, comment_id INT DEFAULT NULL, text LONGTEXT NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB;
ALTER TABLE keywords ADD FOREIGN KEY (comment_id) REFERENCES comments(id);
映射中存在两个问题:
关于
注释id
呢?如何设置注释和关键字之间的关系?注释id是数据库中关键字表中的一个字段,该字段具有该关键字所属注释的id。我想条令会在为评论选择关键词时使用它。关系就像是一个单间(targetEntity=“keywords”,mappedBy=“comment\u id”)对吗?这真的很有效!很多。但我不得不问,单向关系和双向关系有什么区别。同样,在我的例子中,什么实体将是反向的,什么将被拥有。我假设owning和keywodrs中的注释是相反的,但无法说明原因。关键字是拥有方,因为外键“comment\u id”在关键字表中。单向意味着您只能使用Comment::getKeywords()从注释走到关键字。双向意味着您可以使用额外的关键字::getComment()(在我的示例中缺少)向两个方向移动。关联一章相当长,但是您应该阅读“使用关联”和“关联映射”两章来了解整个概念。
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
/**
* @Entity
* @Table(name="comments")
**/
class Comments
{
/** @Id @Column(type="integer") */
private $id;
/** @Column(type="text") */
private $text;
/**
* @OneToMany(targetEntity="keywords", mappedBy="comment")
*/
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;
/**
* @ManyToOne(targetEntity="Comments", inversedBy="keywords")
*/
private $comment;
/**
* @Column(type="text") */
private $text;
public function getText(){return $this->text;}
public function getId(){return $this->id;}
}
CREATE TABLE comments (id INT NOT NULL, text LONGTEXT NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB;
CREATE TABLE keywords (id INT NOT NULL, comment_id INT DEFAULT NULL, text LONGTEXT NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB;
ALTER TABLE keywords ADD FOREIGN KEY (comment_id) REFERENCES comments(id);