【问题标题】:"User follows" with PropelORM - Three way relationship“用户跟随”与 PropelORM - 三向关系
【发布时间】:2015-07-30 15:55:44
【问题描述】:

有人能指出我做“用户关注”之类的正确方向吗?我有 3 个表:users、user_follows 和一个帖子。

如果我为用户对象添加水合物,我可以获得他们关注的用户 ID 数组...并且帖子对象知道是哪个用户发布了它...但是很难仅为给定用户关注的用户获取帖子。

目前有这个,它返回每个人的帖子。

    $posts = PostsQuery::create()
        ->orderByDate('desc')
        ->limit('12')
        ->find();
    return $posts;

需要做filterByXXX()...

【问题讨论】:

标签: php mysql orm propel


【解决方案1】:

Propel ORM 不支持同一张表的实体之间的多对多关系。但是你可以使用EqualNestBehavior 来让它工作。

这样您的代码可能如下所示:

$user = UsersQuery::create()->findPk($userId);

$follows = $user->getFollows();

$posts = PostsQuery::create()
        ->filterByUser($follows)
        ->orderByDate('desc')
        ->limit('12')
        ->find();

这里是架构的相关部分:

<table name="follow">
  <behavior name="equal_nest">
    <parameter name="parent_table" value="users" />
  </behavior>
  <!-- you do not need to specify any colums for the "follow" table, the behavior will add them automatically -->
</table>

【讨论】:

    【解决方案2】:

    这很容易使用use*Query 完成。

    $posts = PostsQuery::create()
        ->useUserFollowsQuery()
            ->filterByUserId([12,34,55])
        ->endUse()
        ->orderByDate('desc')
        ->limit('12')
        ->groupById() //important if you join a one-to-many relation
        ->find();
    return $posts;
    

    查询优化是使用 addJoinCondition:

    ->innerJoinUserFollows()
    ->addJoinCondition('UserFollows', 'UserFollows.user_id = ?',[123,34], Criteria::IN) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-22
      • 2018-02-25
      • 2011-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-11
      • 2015-08-03
      相关资源
      最近更新 更多