php - Doctrine 2 中的问题理解关系映射

标签 php doctrine

我阅读了官方文档和大量线程,但仍然没有找到适合我的情况的解决方案。我的情况非常基本。我有 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;}
}

and how using it is like this:


$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 始终指定在拥有方,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

关于php - Doctrine 2 中的问题理解关系映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3328836/

相关文章:

php - 确定表是否具有连续的主 ID 自动增量值

php - Order By before Group By 使用 Eloquent (Laravel)

doctrine - 从 DQL 中的子查询中选择

symfony - Doctrine - Criteria - expressions - 包含(多对多)

php - Doctrine 查询构建器中的嵌套查询

php - 带标志的搜索功能

php - 如何使用 PHP 从 MYSQL 数据库中获取列类型并将其存储在变量中

PHP array_unique 没有索引

Symfony ManyToOne 关系 getter 返回空对象

mysql - 如何创建 NOT IN where 子句 Doctrine 查询?