Note that there are some explanatory texts on larger screens.

plurals
  1. POhasAndBelongsToMany not pulling data for associationForeignKey
    primarykey
    data
    text
    <p>In my CakePHP application I have users that are able to be friends with other users.</p> <p>This is done with a <code>users</code> table and a <code>friends_users</code> table which has the following columns: <code>id, user_id, friend_id, status</code> for the relationship.</p> <p>So if user id 1 is friends with user id 2 it would have 1 in the user_id and 2 in friend_id columns</p> <p>And the model for User looks like:</p> <pre><code>public $hasAndBelongsToMany = array( 'Friend'=&gt;array( 'className' =&gt; 'User', 'joinTable' =&gt; 'friends_users', 'foreignKey' =&gt; 'user_id', 'associationForeignKey' =&gt; 'friend_id' ) ); </code></pre> <p>When viewing User 1 it works fine and shows User 2 as a friend. BUT when viewing User 2 it does not show User 1 as a friend... So it's not fulling the data when viewed in reverse!</p> <p>Here is the method I use to list the friends for a user:</p> <pre><code>$user = $this-&gt;User-&gt;find('first', array( 'conditions' =&gt; array('User.id' =&gt; $this-&gt;Auth-&gt;user('id')), 'contain'=&gt;'Profile' )); $friends = $this-&gt;User-&gt;find('first', array( 'conditions'=&gt;array( 'User.id'=&gt;$user['User']['id'] ), 'contain'=&gt;array( 'Profile', 'Friend'=&gt;array( 'Profile', 'conditions'=&gt;array( 'FriendsUser.status'=&gt;1 ) ) ) ) ); $this-&gt;set('friends', $friends); </code></pre> <p>Any ideas why this is happening? I have looked through the docs but all seems fine to me.</p> <p><strong>Ideally I'd like to fix my code rather than be pointed to some other resource!</strong></p> <p>Queries on the page: <a href="http://pastebin.com/zQnBVM2X" rel="nofollow">http://pastebin.com/zQnBVM2X</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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