How to map multiple n:m doctrine relationships
This is my relationship: http://i.stack.imgur.com/HYMFJ.png
Hello, I have this relationship, I have mapped one relationship (List_has_Doc) on doctrine with a bidirectional relationship with @ManyToMany but how can I map 开发者_如何学JAVAthe other two ?
Thanks.
Just use as many @ManyToMany relationships as you need. So you could have something like
/**
* @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;
}
Might require to specify @JoinTable in Docs class as well, can't test it now.
精彩评论