Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 处理来自控制器中查找操作的内爆值_Mysql_Arrays_Cakephp_Cakephp 1.3 - Fatal编程技术网

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

Mysql 处理来自控制器中查找操作的内爆值,mysql,arrays,cakephp,cakephp-1.3,Mysql,Arrays,Cakephp,Cakephp 1.3,我相信这里有人能帮我。首先介绍一下我正在做的事情的背景知识。我基本上是在建立一个社交网站,为了便于理解,基本上把它称为大家最喜欢的Facebook的模仿 目前,除一件事外,一切正常。有一件事是把我所有朋友的帖子都贴在墙上。我可以从我请求的朋友和那些请求我的朋友那里获得我的帖子,也可以从那些ID最低的人那里获得帖子 例如,假设我是用户A,有用户B、用户C、用户D和用户E。我登录并请求用户B和用户C,他们都批准。假设用户D和用户E请求我,我批准了它们。现在我是用户A和用户B、C、D和E的朋友。我请求

我相信这里有人能帮我。首先介绍一下我正在做的事情的背景知识。我基本上是在建立一个社交网站,为了便于理解,基本上把它称为大家最喜欢的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之后

现在到代码。。。涉及的模型包括用户、友谊和墙报。My users\u 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”看作一个字符串,而它应该把它们看作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\u to\u id=2,3和$friends\u fromids=4,5,然后我使用以下$aMerge=array\u merge$friends\u to\u id,$friends\u from\u id;把它们结合起来,得到$aMerge=2,3,4,5,这很好

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

$post_conditions =  array(
        'OR' => array(
            'WallPost.user_id' => $this->Auth->user('id'),
            'WallPost.user_id' => $aMerge
        ),
    );
仅返回$aMerge的值,并完全跳过我的用户ID的值。我尝试了多种方法来重新构造此查找条件,但均无效。我肯定我错过了一些小东西,因为它通常是,但我画空白


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

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

这就是我所做的。遵循与查找我自己找到的朋友相同的流程,使用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');
然后,我将其添加到数组\u合并中:

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

然后,当使用$aMerge作为墙柱的查找条件时,我得到了返回值1,2,3,4,5,这正是我所需要的。再次感谢大家的帮助,并移动您的答案,这样我就可以给您评分了……

条件的格式不应该是“field”=>“2,3”,而应该是“field”=>array2,3,因此您只需要使用Set::extract的结果,而不需要内爆。@ori是正确的。通过传递一个数组,Cake在SQL中生成一个IN语句。@ori:最好在其他人之前将其转换为一个答案。比如我;真棒的家伙们,非常感谢你们,在回来检查答案之前,我碰巧发现了这一点,而这正是我所做的。然而,我有一个新的问题与此相关。这在上面粗体文本之后进行了解释。另外,我是新来的,但是你不应该在一个我可以给某人正确答案的领域回答这个问题吗。。。