简体   繁体   English

CakePHP在HABTM关系中的查找查询中返回错误的列

[英]CakePHP returning the wrong column in a find query in a HABTM relationship

I've been wrestling with this find() query for a while. 我一直在努力寻找这个find()查询。 For reference about the tables, see the question here: CakePHP Twitter-clone: Can't get follow-system to work . 有关这些表的参考,请参见此处的问题: CakePHP Twitter-clone:无法使后续系统正常工作

However, in short, this is for a twitter clone I'm doing for self-learning. 但是,简而言之,这是针对我正在自学的Twitter克隆。 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). 作为模型,我有用户,推文和关注者(HABTM用户)。

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: 哪个生成此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 

This is very close, but it's returning the wrong column in my table. 这非常接近,但是返回的是我表中的错误列。 I would like to produce the follow SQL statement: 我想产生以下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 

Any help on how to tweak my find() query would be greatly appreciated! 任何有关如何调整我的find()查询的帮助将不胜感激! Thank you! 谢谢!

Your comment explains it all :D You said that you have something like this: 您的评论说明了所有这件事:D您说过您有这样的事情:

var $hasAndBelongsToMany = array(
   'Follower' => array( 
       'className' => 'Follower',
       'joinTable' => 'user_users',
       'foreignKey' => 'user_id', 
       'associationForeignKey' => 'follower_user_id'
));

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 . 这样,您将告诉cake使用诸如UserUsers类的条件,通过user.id = user_id(查看外键)将用户模型表与user_users UserUsers user_id = 1 THEN it will join user_users table with Follower model table using Follower.id = follower_user_id user_id = 1然后将使用Follower.id = follower_user_id将user_users表与Follower模型表连接

Cake 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. Cake不会使用HABTM或hasMany的joins进行sql查询,因此您发布的只是第一部分,您应该查看第二部分。 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 因此,您得到的是此发现的正常行为,并且没有任何问题,请检查您的sql dump其他部分,您将明白我的意思:D

You can do force join manually , or try doing a behaviour like linkable 您可以手动强制加入 ,也可以尝试执行类似linkable的行为

Hope this solves your question 希望这能解决您的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM