【问题标题】:problem understanding relation mapping in doctrine 2学说2中的问题理解关系映射
【发布时间】:2010-07-25 10:28:35
【问题描述】:

我阅读了官方文档和大量线程,但仍然没有找到适合我的情况的解决方案。我的情况很基本。我有 2 个实体:它们的 cmets 和关键字。一条评论可以有多个关键词,但每个关键词只能用于一条评论。关键字在关键字表中不是唯一的。所以我决定这是一对多的关系。表结构简单如下:

关键字

id          int(11)
comment_id  int(11)
text        varchar(30)

cmets

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 cmets和keywords如何设置关系?
  • comment_id 是关键字表中数据库中的一个字段,该字段具有该关键字所属评论的 id。我想在为 cmets 选择关键字时,学说会使用它。关系就是这样 OneToMany(targetEntity="keywords", mappedBy="comment_id") 对吗?

标签: php doctrine


【解决方案1】:

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, 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 总是在拥有方指定,mappedBy 在反方。

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

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

【讨论】:

  • 这真的有效!非常感谢。但我不得不问单向关系和双向关系有什么区别。此外,在我的情况下,什么实体将是反面以及将拥有什么。我想拥有和关键字的评论是相反的,但不能说为什么。
  • Keywords 是拥有方,因为外键“comment_id”在关键字表中。单向意味着您只能使用 Comment::getKeywords() 从评论走到关键字。双向意味着您可以使用附加的 Keyword::getComment() 双向进行(在我的示例中没有)。关联章节相当长,但您应该阅读“使用关联”和“关联映射”章节来了解整个概念。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多