繁体   English   中英

在理论中理解关系映射的问题2

[英]problem understanding relation mapping in doctrine 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_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? 我的地图是否正确? 我真的卡住了,需要帮助,所以,非常欢迎任何建议。

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);

映射中的两个问题:

  1. 你必须理解拥有和反面之间的区别。 只有一对多的单向关系需要第三个连接表。 但是有一个双向关系,就像在我的映射中拥有属性Keyword :: $ comment一样。
  2. “mappedBy”指的是另一个targetEntity上的属性,即“此关联的另一面”。 InversedBy有一些相同的含义,只是inversedBy总是在拥有方指定,映射在反面。

这听起来非常复杂,但从ORM技术角度来看,这是一种非常有效的方式,因为它允许使用最少数量的所需SQL UPDATE语句更新关联。 请参阅有关反向/拥有如何工作的文档:

http://www.doctrine-project.org/projects/orm/2.0/docs/reference/association-mapping/en#owning-side-and-inverse-side

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM