title: CakePHP Friendship system
body: <p>I am building a friendship system in CakePHP that uses two tables: Users and Friends.</p>
  
  <p>In Users (id,username,email,password) and Friends (id,user_from,user_to,status)</p>
  
  <p>A user requests another user to be friends and this creates a record in the friends table storing both the user ids and setting a status of 'Requested'. The user can either accept the friendship and the status changes to 'Accepted' or cancel the friendship and the record is deleted from the database.</p>
  
  <p>An example link for the request looks like and could be shown either in a users list or on the users details page:</p>
  
  <p><code>&lt;?php echo $this-&gt;Html-&gt;link('Add as Friend', array('controller'=&gt;'friends','action'=&gt;'add_friend',$user['User']['id'])); ?&gt;</code></p>
  
  <p><strong>Question 1</strong> is how could I make this link change to a cancel request link if the user has a request against them or is already friends?</p>
  
  <p>This link corresponds to the following method in the controller:</p>
  
  <pre><code>function add_friend ( $id )
      {
          if(!empty($this-&gt;data))
          {
              $this-&gt;Friend-&gt;Create();
              if($this-&gt;Friend-&gt;save($this-&gt;data))
              {
                  $this-&gt;Session-&gt;setFlash('Friendship Requested');
                  $this-&gt;redirect(array('controller'=&gt;'users','action'=&gt;'login'));
              }
          }
      }
  </code></pre>
  
  <p>So we are passing the ID to the method which will be the <code>user_to</code> and then the 'user_from' needs to be the currently logged in user and set the status to 'Requested'. <strong>Question 2</strong> is how to do I do this? Also how do I prevent a user from creating multiple records by just calling that method over and show a message saying you've already requested friendship.</p>
  
  <p>The next method is:</p>
  
  <pre><code>function accept_friendship ( $id )
      {
       $this-&gt;Session-&gt;setFlash('Friendship Accepted');
                  $this-&gt;redirect(array('controller'=&gt;'friends','action'=&gt;'index'));
              }
          }
      }
  </code></pre>
  
  <p><strong>Question 3:</strong> But again I'm confused as to how I would change the status of the record and mark the users as friends when the method is called. Also need to prevent this from being called multiple times on the same record.</p>
  
  <p>The final bit is listing the friends for the user or another user:</p>
  
  <pre><code>function users_friends( $id )
  {
   $this-&gt;set('friends', $this-&gt;Friend-&gt;find('all'));
  }
  
  function my_friends()
  {
  $this-&gt;set('friends', $this-&gt;Friend-&gt;find('all'));
  }
  </code></pre>
  
  <p>As you can see the first method requires the id of the user you are viewing and then the second method will use the currently logged in user id. <strong>Question 4:</strong> How do I then use this to list the friends of that user?</p>
  
  <p>If anyone can help put me on the right track with this it'd be much appreciated as I've ground to a halt and not sure how to do those 4 things and trying to learn CakePHP as best I can so help is much appreciated. Thanks</p>
  
  <p><strong>EDIT: It has occurred to me that a view with hidden fields could be used to store the information regarding the friend request that the user confirms but this isn't ideal as it means sending the user off somewhere else when in fact I want to just run the function and do the redirect straight off. NOT AJAX THOUGH!</strong></p>