处理控制器中查找操作的内爆值

问题描述 投票:0回答:1

我确信这里有人可以帮助我。首先介绍一下我正在做的事情的背景。我基本上是在建立一个社交网站,为了方便理解,基本上称其为大家最喜欢的 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 之后...

现在看代码...涉及的模型是 User、Friendship 和 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 以及已请求并得到我批准的朋友 ID 的 2 个数组。使用我现在拥有的下面解决方案中给出的相同技术。

$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 的值。我尝试了多种方法来重组这个查找条件,但没有成功。我确信我错过了一些小东西,因为通常都是这样,但我正在画空白......

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

php mysql arrays cakephp cakephp-1.3
1个回答
0
投票

好吧,也许这不是最干净的方法,但我有一个想法,尝试了一下,它起作用了。如果有人有更简单、更干净的解决方案,我将非常感激知道它是什么。

这就是我所做的。遵循与查找我自己找到的朋友相同的流程,使用查找“全部”并将返回值限制为 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),这正是我所需要的。再次感谢大家的帮助并移动您的答案,以便我可以给予您信任......

© www.soinside.com 2019 - 2024. All rights reserved.