php – 问题理解关系映射在原则2
我正在阅读官方文档和大量线程,但仍然找不到我的情况的解决方案.我的情况非常基本.我有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_includesdoctrineORMPersistersBasicEntityPersister.php on line 1096 Notice: Trying to get property of non-object in C:web_includesdoctrineORMPersistersBasicEntityPersister.php on line 1098 Warning: Invalid argument supplied for foreach() in C:web_includesdoctrineORMPersistersBasicEntityPersister.php on line 1098 Notice: Undefined index: comment_id in C:web_includesdoctrineORMPersistentCollection.php on line 168 Fatal error: Call to a member function setValue() on a non-object in C:web_includesdoctrineORMPersistentCollection.php on line 169 哪里不对?应该在哪里定义comment_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;} } 使用Schema Tool可以生成等同于您的架构的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,PRIMARY KEY(id)) ENGINE = InnoDB; ALTER TABLE keywords ADD FOREIGN KEY (comment_id) REFERENCES comments(id); 映射中的两个问题: >你必须了解拥有和反面之间的区别.只需要一对多单向关系就需要第三个连接表.然而,使用双向关系,就像我的映射中拥有的属性Keyword :: $comment一样. 这一切听起来都非常复杂,但它是从ORM技术角度来看非常有效的方法,因为它允许使用最少数量的所需SQL UPDATE语句来更新关联.查看有关逆向/拥有如何工作的文档: http://www.doctrine-project.org/projects/orm/2.0/docs/reference/association-mapping/en#owning-side-and-inverse-side (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |