如何在不执行一长串“OR”操作的情况下找到模型的所有记录?状况?
我在编写返回我正在查找的记录的 CakePHP find() 时遇到问题。
我的关联是这样的:
用户 ->(有很多)->朋友们,
用户->(有很多)->帖子
我正在尝试显示用户所有朋友最近帖子的列表,换句话说,列出当前登录用户的朋友创建的每个帖子。
我能想到的唯一方法就是将所有帖子用户的朋友的 user_ids 在一个大数组中,然后循环遍历每个,以便 find() 调用看起来像这样:
$posts = $this->Post->find('all',array(
'conditions' => array(
'Post.user_id' => array(
'OR' => array(
$user_id_array[0],$user_id_array[1],$user_id_array[2] # .. etc
)
)
)
));
我的印象是,这不是最好的处理方式,就好像该用户很受欢迎一样很多 OR 条件。谁能建议一个更好的选择?
为了澄清,这是我的数据库的简化版本:
“用户”表
编号
用户名
等
“朋友”表
编号
用户 ID
朋友 ID
等
“帖子”表
编号
用户 ID
ETC
I'm having trouble composing a CakePHP find() which returns the records I'm looking for.
My associations go like this:
User ->(has many)-> Friends ,
User ->(has many)-> Posts
I'm trying to display a list of all a user's friends recent posts, in other words, list every post that was created by a friend of the current user logged in.
The only way I can think of doing this is by putting all the user's friends' user_ids in a big array, and then looping through each one, so that the find() call would look something like:
$posts = $this->Post->find('all',array(
'conditions' => array(
'Post.user_id' => array(
'OR' => array(
$user_id_array[0],$user_id_array[1],$user_id_array[2] # .. etc
)
)
)
));
I get the impression this isn't the best way of doing things as if that user is popular that's a lot of OR conditions. Can anyone suggest a better alternative?
To clarify, here is a simplified version of my database:
"Users" table
id
username
etc
"Friends" table
id
user_id
friend_id
etc
"Posts" table
id
user_id
etc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在查看了您重写的内容后,我想我明白您在做什么。您当前的结构将不起作用。帖子里没有提到朋友。因此,根据您发布的架构,朋友无法添加任何帖子。我认为您想要做的是将朋友作为其他用户之一。意思是,用户 FRIEND 实际上只是 USERS 表中的另一个用户。这是一种自引用 HABTM 关系。所以这是我的建议:
1-首先,确保您在数据库中创建了 HABTM 表:
2- 在用户模型中建立关系。
3-使用脚手架插入一些记录,链接好友并添加帖子。
4-在Users控制器中添加查看记录功能:
5- 现在您可以使用以下命令递归查询用户表以提取记录:
6- 您的输出将显示所有记录 (当您 pr($posts) 递归地)将朋友及其帖子包含在返回的数据树中时,
我知道这是一篇很长的帖子,但我认为它将为您尝试做的事情提供最佳解决方案。 CakePHP 的强大功能令人难以置信。正是学习曲线杀死了我们。
快乐编码!
After reviewing what you have rewritten, I think I understand what you are doing. Your current structure will not work. There is no reference in POSTS to friends. So based on the schema you have posted, friends CANNOT add any POSTS. I think what you are trying to do is reference a friend as one of the other users. Meaning, A users FRIEND is actually just another USER in the USERS table. This is a self referential HABTM relationship. So here is what I would propose:
1- First, make sure you have the HABTM table created in the DB:
2- Establish the relationships in the User model.
3- use the scaffolding to insert a few records, linking friends and adding posts.
4- Add the view record function to the Users controller:
5- Now you can query the User table using recursive to pull the records using the following command:
6- Your output will show all of the records (recursively) including the friends and their posts in the returned data tree when you pr($posts)
I know this is a long post, but I think it will provide the best solution for what you are trying to do. The power of CakePHP is incredible. It's the learning curve that kills us.
Happy Coding!
如果
Post.user_id
指向Friend.id
(不遵循 convention 顺便说一句)那么这将导致
.. WHERE Post.user_id IN (1, 2, 3) ..
If
Post.user_id
points toFriend.id
(which wouldn't follow the convention btw) then it would bewhich would result in
.. WHERE Post.user_id IN (1, 2, 3) ..
根据您的设置,运行两个查询可能比尝试通过 Cake 内容将它们链接在一起更快。我建议在 Users 模型中添加类似 getFriendsPosts() 的内容。
然后调用它,在控制器中只需执行
HTH,
特拉维斯
Depending on your setup, it might be quicker to run two queries rather than trying to chain them together via the Cake stuff. I'd recommend adding something like getFriendsPosts() in the Users model.
Then to call it, in the controller just do
HTH,
Travis
CakePHP 不是已经生成了高效的代码吗:
如果没有,你可以这样做
Isn't CakePHP already generating the efficient code of:
if not, you can do
如果您的模型正确关联,Cake 将自动检索相关模型记录。因此,当您搜索特定用户时,Cake 会自动检索相关好友,以及这些好友的相关帖子。您所需要做的就是将递归级别设置得足够高。
您所需要做的就是从用户的朋友那里提取帖子,这很简单:
If your models are properly associated, Cake will automatically retrieve related model records. So, when you search for a specific user, Cake will automatically retrieve related friends, and related posts of these friends. All you need to do is set the recursion level high enough.
All you need to do is extract the posts from the user's friends, which is as easy as: