CakePHP 在 HABTM 关系中的查找查询中返回错误的列
我已经为这个 find() 查询苦苦挣扎了一段时间。有关表格的参考,请参阅此处的问题:CakePHP Twitter-克隆:无法让后续系统工作。
不过,简而言之,这是我为了自学而做的一个 Twitter 克隆。我有三张桌子。
Users (id, name)
Tweets (id, user_id, content, date)
UserUsers (id, user_id, follower_user_id)
作为模特,我有用户、推文和关注者(HABTM 用户)。
现在,我有这个查找查询:
$this->User->bindModel(array('hasOne' => array('UserUsers')));
$fields = array('User.username');
$conditions = array(
'UserUsers.user_id'=> $current_id);
$contain = $this->User->contain(array(
'UserUsers',
'Tweet' => array(
'fields'=> array('Tweet.content', 'Tweet.date'),
'order' => 'Tweet.date DESC',
'limit' => 1)));
$data = $this->User->find('all', compact('fields', 'conditions', 'contain'));
它生成这个 SQL 语句,这几乎就是我想要的:
SELECT `User`.`username`, `User`.`id` FROM `users` AS `User` LEFT JOIN `user_users` AS `UserUsers` ON (`UserUsers`.`user_id` = `User`.`id`) WHERE `UserUsers`.`user_id` = 1
这非常接近,但它在我的表中返回错误的列。我想生成以下 SQL 语句:
SELECT `User`.`username`, `User`.`id` FROM `users` AS `User` LEFT JOIN `user_users` AS `UserUsers` ON (`UserUsers`.`follower_user_id` = `User`.`id`) WHERE `UserUsers`.`user_id` = 1
任何有关如何调整我的 find() 查询的帮助将不胜感激!谢谢你!
I've been wrestling with this find() query for a while. For reference about the tables, see the question here: CakePHP Twitter-clone: Can't get follow-system to work.
However, in short, this is for a twitter clone I'm doing for self-learning. I have three tables.
Users (id, name)
Tweets (id, user_id, content, date)
UserUsers (id, user_id, follower_user_id)
As models, I have Users, Tweets, and Followers (HABTM users).
Right now, I have this find query:
$this->User->bindModel(array('hasOne' => array('UserUsers')));
$fields = array('User.username');
$conditions = array(
'UserUsers.user_id'=> $current_id);
$contain = $this->User->contain(array(
'UserUsers',
'Tweet' => array(
'fields'=> array('Tweet.content', 'Tweet.date'),
'order' => 'Tweet.date DESC',
'limit' => 1)));
$data = $this->User->find('all', compact('fields', 'conditions', 'contain'));
Which produces this SQL statement, which is ALMOST what I want:
SELECT `User`.`username`, `User`.`id` FROM `users` AS `User` LEFT JOIN `user_users` AS `UserUsers` ON (`UserUsers`.`user_id` = `User`.`id`) WHERE `UserUsers`.`user_id` = 1
This is very close, but it's returning the wrong column in my table. I would like to produce the follow SQL statement:
SELECT `User`.`username`, `User`.`id` FROM `users` AS `User` LEFT JOIN `user_users` AS `UserUsers` ON (`UserUsers`.`follower_user_id` = `User`.`id`) WHERE `UserUsers`.`user_id` = 1
Any help on how to tweak my find() query would be greatly appreciated! Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的评论说明了一切:D
你说你有这样的东西:
通过这个,你告诉cake使用
UserUsers
等条件通过user.id = user_id(查看外键)将用户模型表与user_users连接起来。< code>user_id = 1 那么它将使用 Follower.id = follower_user_id 将 user_users 表与 Follower 模型表连接起来Cake 不会使用 HABTM 或 hasMany 的连接执行 sql 查询,那又怎样您发布的只是第一部分,您应该看看第二部分。所以你得到的是这个发现的正常行为,没有任何问题,检查你的sql转储的其他部分,你会明白我的意思:D
你可以做 手动强制加入,或者尝试执行类似 可链接
希望这能解决您的问题
Your comment explains it all :D
You said that you have something like this:
With this you are telling cake to join the user model table with user_users by user.id = user_id (look at the foreign key) using your conditions like
UserUsers
.user_id
= 1 THEN it will join user_users table with Follower model table using Follower.id = follower_user_idCake won't do a sql query with joins for HABTM or hasMany, so what you post is just the first part, you should take a look for the second part. So what you are getting IS the normal behaviour for this find and there is nothing wrong with it, check your sql dump for the other part and you will see what i mean :D
You can do force join manually, or try doing a behaviour like linkable
Hope this solves your question