title: How do I remove a self referencing n:n relation in Doctrine?
body: <p>I'd like to represent e friend relationship between two users in Doctrine. I am able to create the relationship, but I don't know what's the best way to delete it.</p>
  
  <p>I have the following schema:</p>
  
  <pre><code>User:
    columns:
      name: string(255)
    relations:
      Friends:
        class: User
        local: user1
        foreign: user2
        refClass: FriendReference
        equal: true
  
  FriendReference:
      columns:
          user1:
              type: integer
              primary: true
          user2:
              type: integer
              primary: true    
  </code></pre>
  
  <p>Here's how I create the relationsship:</p>
  
  <pre><code>$user1 = new User();
  $user2 = new User();
  $user1-&gt;Friends[] = $user2;
  </code></pre>
  
  <p>This works perfectly.</p>
  
  <pre><code>mysql&gt; select * from friend_reference;
  +-------+-------+
  | user1 | user2 |
  +-------+-------+
  |     4 |     5 |
  +-------+-------+
  1 row in set (0.01 sec)
  </code></pre>
  
  <p>Now what's the best way to remove the relationship if I have the two primary keys 4 and 5 of the relationship to delete? I could</p>
  
  <ol>
  <li>Fetch the object with the ID 4 and Iterate through <code>$user-&gt;Friends</code> and then use <code>unlink</code> to delete this relationship.</li>
  <li><p>Write the following query</p>
  
  <pre><code>$query = Doctrine_Query::create()
     -&gt;delete('FriendReference')
     -&gt;where('(user1=4 AND user2=5) OR (user2=4 AND user1=5)')
     -&gt;execute();
  </code></pre></li>
  </ol>
  
  <p>I think both options neither elegant nor performant.</p>