在学说中建立朋友关系的最佳方式是什么。
我的方式如下。
用户.xml.orgm:
<many-to-many field="friends" target-entity="MyEntityUser">
<join-table name="my_friends">
<join-columns>
<join-column name="idAUsers" referenced-column-name="id" on-delete="CASCADE" nullable="false" />
</join-columns>
<inverse-join-columns>
<join-column name="idBUsers" referenced-column-name="id" on-delete="CASCADE" nullable="false" />
</inverse-join-columns>
</join-table>
</many-to-many>
用户.php
public function getFriends() {
$friends = $this->getAUsers();
foreach ($this->getBUsers() as $bUser) {
$friends[] = $bUser;
}
return $friends;
}
正如你所看到的,我需要合并两个UserFriend,每次创建查询时都加入。
您应该使用自引用多对多。
理论文档的例子似乎非常合适,因为它正是处理你的场景。