title: Cakephp update or add new record
body: <p>I have an image upload that adds the filename to a table called attachments. If the id already exists then I want it to update and if not then create a new record. At the moment it creates a new record so I have multiple records forthe one id. The id's are from a table called Addon's.</p>
  
  <p>I am not sure how to do this in cakephp.</p>
  
  <pre><code>    if (!empty($this-&gt;data)) {
          $this-&gt;layout = null;
          //if(empty($this-&gt;data['AddOn']['id'])){unset($this-&gt;data['AddOn']);}
  
          // restructure data for uploader plugin // NEED TO GET RID OF THIS ? MOVE IT
          $tmp_file = $this-&gt;data['Attachment'][0]['file'];
          $tmp_file['extension'] =  array_reverse(explode('.', $tmp_file['name']));
          $tmp_file['extension'] = $tmp_file['extension'][0];
          $tmp_file['title'] = strtolower(substr($tmp_file['name'],0,(0-strlen('.'.$tmp_file['extension']))));
          $this-&gt;data['Attachment'][0]['alternative'] = ucwords(str_replace('_',' ', $tmp_file['title']));
  
          $previous = $this-&gt;AddOn-&gt;Attachment-&gt;find('first', array('conditions'=&gt; array('model'=&gt;'AddOn', 'foreign_key'=&gt;$id)));
          if( !empty( $previous ) ) {
              $this-&gt;AddOn-&gt;Attachment-&gt;id = $previous[ 'Attachment' ][ 'id' ];
          }
  
          if ($this-&gt;AddOn-&gt;save($this-&gt;data, array('validate' =&gt; 'first'))) {    
  
              $id = $this-&gt;AddOn-&gt;Attachment-&gt;getLastInsertID();
  
              $att = $this-&gt;AddOn-&gt;Attachment-&gt;query("SELECT * from attachments WHERE id = ".$id);
              $this-&gt;set('attachment',$att[0]['attachments']);
  
          } else {
              $tmp_file['name'] = 'INVALID FILE TYPE';
          }
  
  
          //debug($this-&gt;data);
          $this-&gt;set('file', $tmp_file);
          $this-&gt;RequestHandler-&gt;renderAs($this, 'ajax');
          $this-&gt;render('../elements/ajax');
  
      }
  </code></pre>