我确定这里有人可以帮助我。首先关于我在做什么的背景。我基本上建立了一个社交网站,为了便于理解,基本上把它称为每个人最喜欢的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。我的用户控制器在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),用户与1. ID(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)。我怎么能解决这个问题是没有找到我,再次感谢任何帮助。
感谢您的帮助球员,这是正确的答案。这是我关于这个问题的下一个小问题。
因此,whats从DB被拉正在查询对三件事情,我的用户ID,并包含2个数组的请求并批准朋友的ID,并要求和批准了我的朋友的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的价值。我尝试了多种方式来重组这种查找条件,但无济于事。我敢肯定,我失去了一些东西小,这就是它通常是,但我画坯......
确保你回答,我可以将其标记...
条件不应该格式为''字段'=>'2,3',但''字段'=> array(2,3)',所以你只需要使用'Set :: extract'的结果而不会使它崩溃。 – ori
@ori是正确的。通过传递数组,Cake在SQL中生成一个'IN'语句。 – jeremyharris
@ori:最好在别人做出这个答案之前做出回答。像我这样的例子;) –