title: PhalconPHP Framework - How to update a model object and the related object
body: <p>I am working with <strong>PhalconPHP</strong> and so far I have had little problems. </p>
  
  <p>The framework is very good. </p>
  
  <p>However, I've run into a problem that I haven't been able to sort out. It's probably a very simple thing but I've been running in circles, and couldn't find any reference to this problem.</p>
  
  <p>The problem is I cannot update a model object if I also update a related entity.</p>
  
  <p>For example, let's suppose I have a couple of model classes.</p>
  
  <p><strong>Contact:</strong></p>
  
  <pre><code>class Contact extends \Phalcon\Mvc\Model
  {
      public function initialize()
      {
          $this-&gt;belongsTo('email_id', 'Email', 'id', ['alias' =&gt; 'Email']);
      }
  }
  </code></pre>
  
  <p><strong>And Email:</strong></p>
  
  <pre><code>class Email extends \Phalcon\Mvc\Model
  {
      public function initialize()
      {
          $this-&gt;hasMany('id', 'Contact', 'email_id', ['alias' =&gt; 'Contacts']);
      }
  }
  </code></pre>
  
  <p>When I try to create a new <code>Contact</code> and <code>Email</code> the following works:</p>
  
  <pre><code>$contact = new Contact();
  // ... assign other fields here (i.e. name)
  $email = new Email();
  // ... assign other email fields here (i.e. address)
  $contact-&gt;setEmail($email); // or $contact-&gt;email = $email;
  $contact-&gt;save();
  // record created
  </code></pre>
  
  <p>However, when I try to update a <code>Contact</code> and change the <code>Email</code> relation, the information is just not updated:</p>
  
  <pre><code>// Only update other fields
  $contact-&gt;name = 'Some other name';
  $contact-&gt;save();    // This works
  
  // Update the related object
  $contact-&gt;name = 'A new name';
  $contact-&gt;setEmail($anotherValidEmail);
  $contact-&gt;save();    // This doesn't work! Not even the name field is updated
  </code></pre>
  
  <p>I have tried using <code>update()</code> instead of <code>save()</code>.</p>
  
  <p>I have tried using <code>$contact-&gt;email = $newEmailObject</code> as well, but I get the same results.</p>
  
  <p>Have anyone ran into this problem? What am I doing wrong?</p>
  
  <p>I'm using <strong>Mac OS X</strong>, PhalconPHP 1.2.3</p>
  
  <p>I've also tested it on Windows 8, with PhalconPHP 1.1.0</p>
  
  <p>Any help is appreciated.</p>
  
  <p><strong>updated</strong></p>
  
  <p>I have printed the results of $contact->getMessages() after save(), but I get no results. It's as if save() was successful, however, no SQL statement is executed.</p>
  
  <p><strong>LAST UPDATE: Problem replicated and Workaround found!</strong></p>
  
  <p>We have been able to replicate the problem. We are doing this:</p>
  
  <pre><code>$contact = Contact::findFirst(123);
  if ($contact-&gt;email-&gt;emailaddress != $newaddress) {
      $email = new Email();
      $email-&gt;emailaddress = $newaddress;
  
      $contact-&gt;email = $email;
      $contact-&gt;save();
  }
  </code></pre>
  
  <p>This DOES NOT WORK! Once we compare the emailaddress field from the email related object, to the string for the new address saving does not work.</p>
  
  <p>However, if we modify the code a little bit and do this:</p>
  
  <pre><code>  $contact = Contact::findFirst(123);
    if ($contact-&gt;email-&gt;emailaddress != $newaddress) {
      $email = new Email();
      $email-&gt;emailaddress = $newaddress;
  
      // Load the record (again!)
      $contact = Contact::findFirst(123);
      $contact-&gt;email = $email;
      $contact-&gt;save();
    }
  </code></pre>
  
  <p>This actually works.</p>
  
  <p>I guess that loading the related object affects the ability to update that particular field, and finding the contact object again, flushes the related object.</p>
  
  <p>So, we have a workaround that I hope will help other people facing the same problem.</p>