Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is an interesting problem, I was faced with this same issue a few years ago when I was building a dating site.</p> <p>What you need to do is have (in my case) 4 sets of relations:</p> <ul> <li>Forward relations</li> <li>Forward relations (unapproved friendships)</li> <li>Backward relations</li> <li>Backward relations (unapproved friendships)</li> </ul> <p>This will ensure you pull all necessary data, use <code>containable</code> wherever possible.</p> <pre class="lang-php prettyprint-override"><code>var $hasAndBelongsToMany = array( 'ForwardRelation' =&gt; array( 'className' =&gt; 'User', 'joinTable' =&gt; 'friends_users', 'foreignKey' =&gt; 'user_id', 'conditions' =&gt; array('approved' =&gt; '1'), 'associationForeignKey' =&gt; 'friend_id', ), 'ForwardRelationUn' =&gt; array( 'className' =&gt; 'User', 'joinTable' =&gt; 'friends_users', 'foreignKey' =&gt; 'user_id', 'conditions' =&gt; array('approved' =&gt; '0'), 'associationForeignKey' =&gt; 'friend_id' ), 'BackRelation' =&gt; array( 'className' =&gt; 'User', 'joinTable' =&gt; 'friends_users', 'foreignKey' =&gt; 'friend_id', 'associationForeignKey' =&gt; 'user_id', 'conditions' =&gt; array('approved' =&gt; 1) ), 'BackRelationUn' =&gt; array( 'className' =&gt; 'User', 'joinTable' =&gt; 'friends_users', 'foreignKey' =&gt; 'friend_id', 'associationForeignKey' =&gt; 'user_id', 'conditions' =&gt; array('approved' =&gt; '0') ) ); </code></pre> <p>Add a behavior with some <code>afterFind()</code> magic, and you will be golden!</p> <p>This is my function to find friends:</p> <pre class="lang-php prettyprint-override"><code>function getFriends( $userid, $search = null ) { $Friends = ClassRegistry::init( 'FriendsUser' ); $Friends-&gt;bindModel( array( 'belongsTo' =&gt; array( 'User' =&gt; array( 'className' =&gt; 'User', 'foreignKey' =&gt; 'user_id' ), 'Friend' =&gt; array( 'className' =&gt; 'User', 'foreignKey' =&gt; 'friend_id' ) ) ) ); $data = $Friends-&gt;find( 'all', array( 'fields' =&gt; array( 'DISTINCT FriendsUser.friend_id', 'FriendsUser.user_id', 'FriendsUser.approved', 'User.id', 'User.username', 'Friend.id', 'Friend.username' ), 'conditions' =&gt; array( 'AND' =&gt; array( 'OR' =&gt; array( 'Friend.username LIKE' =&gt; '%' . $search . '%', 'User.username LIKE' =&gt; '%' . $search . '%' ) ) ), 'contain' =&gt; array( 'User', 'Friend' ), 'order' =&gt; array( 'User.username ASC', 'FriendsUser.approved DESC' ) ) ); $friendlist = array(); $i = 0; foreach ( $data as $datum ) { foreach ( $datum as $key =&gt; $value ) { if ( in_array( $key, array( 'User', 'Friend' ) ) ) { if ( $value['id'] == $userid ) { $friendlist[$i]['Me'] = $value; } else { $friendlist[$i]['Friend'] = $value; } } } $i++; } $matches = array(); foreach ( $friendlist as $friend ) { if ( stripos( $friend['Friend']['username'], $search ) !== false ) $matches[ $friend['Friend']['id'] ] = $friend['Friend']['username']; } asort( $matches ); $i = 0; $newmatches = array(); foreach ( $matches as $key =&gt; $match ) { $newmatches[$i]['Tag']['caption'] = $match; $newmatches[$i]['Tag']['value'] = $key; $newmatches[$i]['Tag']['image'] = $this-&gt;getProfileImage( $key ); $i++; } return $newmatches; } </code></pre>
 

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