【问题标题】:Best practice doctrine user friend relationship最佳实践学说用户朋友关系
【发布时间】:2015-10-28 17:18:01
【问题描述】:

在教义中建立朋友关系的最佳方式是什么。

我的方式是。

用户.xml.orm:

    <many-to-many field="friends" target-entity="MyEntity\User">
        <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 并每次都加入。

【问题讨论】:

    标签: symfony doctrine-orm doctrine


    【解决方案1】:

    您应该使用自引用多对多。

    doctrine documentation example 似乎非常贴切,因为它正在处理您的场景。

    【讨论】:

    • 谢谢。我可以使用它,但当我正确理解这一点时,它更像是 Twitter 关注者关系而不是 facebook 朋友关系。我确实使用了它,当它在 myFriend 和friendWithMe 中时,我只在我的api中显示为朋友,否则它是一个朋友请求,发送(仅在朋友中)或接收(仅在friendWithMe中)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-05
    • 1970-01-01
    • 2013-02-10
    • 1970-01-01
    • 2010-09-22
    相关资源
    最近更新 更多