Doctrine 2 join table + extra fields

时光总嘲笑我的痴心妄想 提交于 2019-12-18 14:14:32

问题


I've got two tables and a join table: 'staff', 'classification' and 'staff_classification'. In the join table I've got an extra boolean field: 'showclassification'. My annotation is as follows:

 /**
 * @ManyToMany(targetEntity="Staff", inversedBy="classifications")
 * @JoinTable(name="staff_classifications",
 *  joinColumns={@JoinColumn(name="staffid", referencedColumnName="id")},
 *  inverseJoinColumns={@JoinColumn(name="classificationid", referencedColumnName="id", unique=true)});
 */
  1. How do I add the extra field 'showclassifications' to the join table?
  2. How do I reference the field via DQL? E.g. What query would get all of a staff's classifications that are allowed to be shown?
  3. Do I place the above annotation in one class and a @ManyToMany annotation with no @joinTable in the other? E.g. @ManyToMany (targetEntity="Classification")?

回答1:


You want an entity that describes the relationship (StaffClassifications), which has OneToMany relationships with both staff and classifications.

ManyToMany doesn't allow you have any "extra" properties, because the join table is not an entity, and thus can't have any properties.



来源:https://stackoverflow.com/questions/4435709/doctrine-2-join-table-extra-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!