学说2中的问题理解关系映射

2022-01-16 00:00:00 php doctrine

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

I read official documentation and tons of threads but still do not find solution for my situation. My case is very basic. I have 2 entities: comments and keywords for them. One comment can have many keywords but each keyword is only for one comment. Keywords are not unique in keyword table. So i decided this is one-to-many relation. Tables structure are simply like follows:

关键字

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

评论

id      int(11)
text    text

这是我绘制它们的方式:

here is how i mapped them:


/**
 *  @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;}
}

以及如何使用它是这样的:

and how using it is like this:


$comments = $this->em->getRepository('comments' )->findAll();
foreach($comments as $comment){
    foreach($comment->getKeywords() as $keyword){
        $keyword->getText();
    }
}

并得到这个错误:<代码>

and got this errors:


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?我的映射正确吗?我真的卡住了,需要帮助,所以非常欢迎任何建议.

What is wrong? where is should define comment_id? Is my mapping correct? i really stuck and need help so please, any advice are very welcome.

推荐答案

mappedBy 属性并没有说明外键的名称,这就是@JoinColumn"注解的用途.这种情况的正确映射是:

The mappedBy attribute does say nothing about the name of the foreign key, That is what the "@JoinColumn" annotation is for. The correct mapping for this scenario would be:

/**
 *  @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:

Using Schema Tool it generates the SQL which equals your schema:

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 工作原理的文档:

This all sounds very complicated, but its a very efficient way from the ORMs technical perspective, because it allows to update associations with the least number of required SQL UPDATE statements. See the docs on how Inverse/Owning works:

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

相关文章