如何映射多个n:m学说关系

时间:2011-08-09 01:31:34

标签: php mysql database doctrine

这是我的关系:http://i.stack.imgur.com/HYMFJ.png

你好,我有这种关系,我已经在学说上映射了一个与@ManyToMany双向关系的关系(List_has_Doc),但我怎样才能映射其他两个?

感谢。

1 个答案:

答案 0 :(得分:0)

根据需要使用尽可能多的@ManyToMany关系。所以你可以有像

这样的东西
/**
 * @Entity
 * @Table(name="list")
 */
class List{
   //other definitions

   /**
    * @ManyToMany(targetEntity="Doc", invertedBy="flaggedDocList")
    * @JoinTable(name="List_flagged_Doc",
    *      joinColumns={@JoinColumn(name="flagged_doc_id", referencedColumnName="id")},
    *      inverseJoinColumns={@JoinColumn(name="flagged_list_id", referencedColumnName="id")}
    * )
    */
    protected $flaggedDocs;


   /**
    * @ManyToMany(targetEntity="Doc", invertedBy="requestedDocList")
    * @JoinTable(name="List_requested_Doc",
    *      joinColumns={@JoinColumn(name="requested_doc_id", referencedColumnName="id")},
    *      inverseJoinColumns={@JoinColumn(name="requested_list_id", referencedColumnName="id")}
    * )
    */
    protected $requestedDocs;

   /**
    * @ManyToMany(targetEntity="Doc", invertedBy="ownedDocList")
    * @JoinTable(name="List_owned_Doc",
    *      joinColumns={@JoinColumn(name="owned_doc_id", referencedColumnName="id")},
    *      inverseJoinColumns={@JoinColumn(name="owned_list_id", referencedColumnName="id")}
    * )
    */
    protected $ownedDocs;
}


/**
 * @Entity
 * @Table(name="doc")
 */
class Doc{
    //...other definitions
    /**
     * @ManyToMany(targetEntity="List", mappedBy="flaggedDocs")
     */
     protected $flaggedDocList;

    /**
     * @ManyToMany(targetEntity="List", mappedBy="requestedDocs")
     */
     protected $requestedDocList;


    /**
     * @ManyToMany(targetEntity="List", mappedBy="ownedDocs")
     */
     protected $ownedDocList;
}

也可能需要在Docs类中指定@JoinTable,现在无法测试。