Note that there are some explanatory texts on larger screens.

plurals
  1. POCakePHP returning the wrong column in a find query in a HABTM relationship
    text
    copied!<p>I've been wrestling with this find() query for a while. For reference about the tables, see the question here: <a href="https://stackoverflow.com/questions/7671525/cakephp-twitter-clone-cant-get-follow-system-to-work">CakePHP Twitter-clone: Can&#39;t get follow-system to work</a>.</p> <p>However, in short, this is for a twitter clone I'm doing for self-learning. I have three tables. </p> <pre><code>Users (id, name) Tweets (id, user_id, content, date) UserUsers (id, user_id, follower_user_id) </code></pre> <p>As models, I have Users, Tweets, and Followers (HABTM users).</p> <p>Right now, I have this find query:</p> <pre><code>$this-&gt;User-&gt;bindModel(array('hasOne' =&gt; array('UserUsers'))); $fields = array('User.username'); $conditions = array( 'UserUsers.user_id'=&gt; $current_id); $contain = $this-&gt;User-&gt;contain(array( 'UserUsers', 'Tweet' =&gt; array( 'fields'=&gt; array('Tweet.content', 'Tweet.date'), 'order' =&gt; 'Tweet.date DESC', 'limit' =&gt; 1))); $data = $this-&gt;User-&gt;find('all', compact('fields', 'conditions', 'contain')); </code></pre> <p>Which produces this SQL statement, which is ALMOST what I want:</p> <pre><code>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 </code></pre> <p>This is very close, but it's returning the wrong column in my table. I would like to produce the follow SQL statement:</p> <pre><code>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 </code></pre> <p>Any help on how to tweak my find() query would be greatly appreciated! Thank you!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload