title: Using relational AR to retrieve "OR" condition
body: <p>I'm using the Yii framework and have come across an issue whilst building a Friend/Connection style system. I have a standard users table and to store friends I have a friends table with the following layout (the appropriate FKs etc have been set up):</p>
  
  <pre><code>id | userId | friendId | and some other fields...
  </code></pre>
  
  <p>As the "friend" is mutual - so, if user A is a friend with user B, then user B is a friend with user A - therefore there is only ever one entry per "friendship".</p>
  
  <p>What I'm trying to do is use Yii's relational query to eventually return items which belong to friends. Currently I have the following relation in the User model...</p>
  
  <pre><code>'friends' =&gt; array(self::HAS_MANY, 'UserFriend', 'userId'),
  </code></pre>
  
  <p>However that only returns 'friends' if the user's ID is present in the userId field. If the user's ID is in the friendId field then the "friendship" won't be recognised.</p>
  
  <p>Is there a way to query using "OR" - for example: if user's ID == userId OR user's ID == friendId then return that entry?</p>
  
  <p>In addition, is there a way to get Yii to return the other ID, so if user's ID == userId it will return friendId, else if user's ID == friendId it will return userId?</p>
  
  <p>I'm eventually trying to do something like:</p>
  
  <pre><code>$userModel = User::model()-&gt;findByPk(Yii::app()-&gt;user-&gt;id); // user's model
  $userFriends = $userModel-&gt;with(items)-&gt;friends; // return friends with their items
  </code></pre>
  
  <p>Hopefully I haven't confused too many! Sorry for the poor explanation</p>
  
  <p>Thanks :)</p>