簡體   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