0

我相信这里有人可以帮助我。首先介绍一下我在做什么的背景。我基本上是在建立一个社交网站,为了便于理解,基本上称它为每个人最喜欢的 Facebook 的模拟。

目前一切正常,除了一件事。一件事是在“墙上”显示我所有朋友的帖子。我可以获取我的帖子,也可以从我请求的朋友和那些请求我的朋友那里获取 ID 最低的人的帖子。

例如,假设我是用户 A,有用户 B、用户 C、用户 D 和用户 E。我登录并请求用户 B 和用户 C,他们都批准了。让我们假设用户 D 和用户 E 请求我并且我批准了他们两个。所以现在我(用户 A)是用户 B、C、D 和 E 的朋友。我请求 B 和 C,D 和 E 请求我。为了这个例子,我们假设这些用户的 ID 是按顺序排列的;1是我(A),通过5是E。

当“墙”出现时,我会收到我的帖子,这是意料之中的,但我只收到来自用户 B(我请求)和用户 D(请求我)的帖子。用户 C 和 E 的帖子不显示,因为他们的 ID 在 B 和 D 之后...

现在到代码...涉及的模型是用户、友谊和 WallPost。我的 users_controller 在 profile() 函数中有以下查找操作。

   $friends_to = $this->Friendship->find('all', array(
          'conditions' => array('Friendship.pending' => 1, 'Friendship.approved' => 1, 'UserTo.id' => $id),
          'fields' => array('Friendship.user_from')
        )
    );  
    $friends_to_ids = implode(',', Set::extract($friends_to, '{n}.Friendship.user_from'));

    $friends_from = $this->Friendship->find('all', array(
          'conditions' => array('Friendship.pending' => 1, 'Friendship.approved' => 1, 'UserFrom.id' => $id),
          'fields' => array('Friendship.user_to')
        )
    );
    $friends_from_ids = implode(',', Set::extract($friends_from, '{n}.Friendship.user_to'));

    $post_conditions =  array(
        'OR' => array(
            array('WallPost.user_id' => $this->Auth->user('id')),
            array('WallPost.user_id' => $friends_to_ids),
            array('WallPost.user_id' => $friends_from_ids)
        ),
    );
    $posts = $this->WallPost->find('all', array(
          'conditions' => $post_conditions,
          'order' => array('WallPost.created' => 'desc')
        )
    );
    $this -> set ('posts', $posts);

在我上面给出的示例的上下文中,这将返回以下 SQL 语句:

SELECT `WallPost`.`id`, `WallPost`.`user_id`, `WallPost`.`content`, `WallPost`.`created`, `WallPost`.`modified`, `User`.`id`, `User`.`first_name`, `User`.`last_name`, `User`.`username`, `User`.`password`, `User`.`email`, `User`.`group_id`, `User`.`confirmed`, `User`.`confirm_code`, `User`.`created`, `User`.`modified` FROM `wall_posts` AS `WallPost` LEFT JOIN `users` AS `User` ON (`WallPost`.`user_id` = `User`.`id`) WHERE ((`WallPost`.`user_id` = 1) OR (`WallPost`.`user_id` = '2,3') OR (`WallPost`.`user_id` = '4,5')) ORDER BY `WallPost`.`created` desc

所以 ID 在那里,但只显示以下内容。( WallPost. user_id= 1),ID 为 1 的用户。( WallPost. user_id= '2,3'),仅显示用户 ID 2。( WallPost. user_id= '4,5'),仅显示用户 ID 4。

我在这里做错了什么?为什么不显示所有这些用户的结果?任何朝着正确方向的推动将不胜感激。

快速更新:我重组了我的查询,所以它有点不同但返回相同的东西。

SELECT `WallPost`.`id`, `WallPost`.`user_id`, `WallPost`.`content`, `WallPost`.`created`, `WallPost`.`modified`, `User`.`id`, `User`.`first_name`, `User`.`last_name`, `User`.`username`, `User`.`password`, `User`.`email`, `User`.`group_id`, `User`.`confirmed`, `User`.`confirm_code`, `User`.`created`, `User`.`modified` FROM `wall_posts` AS `WallPost` LEFT JOIN `users` AS `User` ON (`WallPost`.`user_id` = `User`.`id`) WHERE `WallPost`.`user_id` IN (1, '2,3', '4,5') ORDER BY `WallPost`.`created` desc

经过更多研究后,它似乎将“2,3”和“4,5”视为字符串,而应该将它们视为IN(1,2,3,4,5)的所有内容。我无法解决这个问题,再次感谢任何帮助。

感谢您提供正确答案的帮助。现在这是我关于这个问题的下一个小问题。

因此,从数据库中提取的内容正在针对 3 个事物进行查询,即我的用户 id,以及包含请求和批准的朋友 id 的 2 个数组,以及我请求和批准的朋友的 id。使用下面我现在的解决方案中给出的相同技术。

$friends_tos = $this->Friendship->find('all', array(
          'conditions' => array('Friendship.pending' => 1, 'Friendship.approved' => 1, 'UserTo.id' => $id),
          'fields' => array('Friendship.user_from'), //array of field names
        )
    );  
    $friends_to_ids = Set::extract($friends_tos, '{n}.Friendship.user_from');

    $friends_froms = $this->Friendship->find('all', array(
          'conditions' => array('Friendship.pending' => 1, 'Friendship.approved' => 1, 'UserFrom.id' => $id),
          'fields' => array('Friendship.user_to'), //array of field names
        )
    );
    $friends_from_ids = Set::extract($friends_froms, '{n}.Friendship.user_to');

这给了我 $friends_to_ids = (2,3) 和 $friends_from)ids = (4,5),然后我使用以下$aMerge = array_merge($friends_to_ids, $friends_from_ids);将它们组合起来,得到 $aMerge = (2,3,4,5),这很棒.

但是使用它作为我的查找条件:

$post_conditions =  array(
        'OR' => array(
            'WallPost.user_id' => $this->Auth->user('id'),
            'WallPost.user_id' => $aMerge
        ),
    );

仅返回 $aMerge 的值并完全跳过我的用户 ID 的值。我尝试了多种方法来重组这个查找条件,但无济于事。我确定我错过了一些小东西,因为它通常是这样的,但我正在画空白......

确保你回答我可以标记它的地方......

4

1 回答 1

0

好的,所以也许这不是最干净的方法,但我有一个想法,尝试过,它奏效了。如果有人有更简单和更清洁的解决方案,我将不胜感激知道它是什么。

所以这就是我所做的。遵循与我找到自己的朋友相同的流程,使用 find 'all' 并将返回值限制为 1。然后以数组的形式提取该 ID,即使它是单个值。

$me = $this->User->find('all', array(
          'conditions' => array('User.id' => $this->Auth->user('id')),
          'fields' => array('User.id'),
          'limit' => 1,
        )
    );
    $my_id = Set::extract($me, '{n}.User.id');

然后我将它添加到array_merge:

$aMerge = array_merge($my_id, $friends_to_ids, $friends_from_ids);

然后,当使用 $aMerge 作为“墙”帖子的查找条件时,我得到了返回 (1,2,3,4,5),这正是我所需要的。再次感谢所有的帮助并移动您的答案,以便我可以给予您的信任...

于 2012-05-02T13:42:01.833 回答