I work with PhalconPHP and so far I have had few problems.
The frame is very good.
However, I ran into a problem that I could not figure out. This is probably a very simple thing, but I worked in circles and could not find links to this problem.
The problem is that I cannot update the model object if I also update the associated object.
For example, suppose I have a couple of model classes.
Contact:
class Contact extends \Phalcon\Mvc\Model { public function initialize() { $this->belongsTo('email_id', 'Email', 'id', ['alias' => 'Email']); } }
And email:
class Email extends \Phalcon\Mvc\Model { public function initialize() { $this->hasMany('id', 'Contact', 'email_id', ['alias' => 'Contacts']); } }
When I try to create new Contact and Email , the following works:
$contact = new Contact(); // ... assign other fields here (ie name) $email = new Email(); // ... assign other email fields here (ie address) $contact->setEmail($email); // or $contact->email = $email; $contact->save(); // record created
However, when I try to update Contact and change the Email relationship, the information simply does not update:
// Only update other fields $contact->name = 'Some other name'; $contact->save(); // This works // Update the related object $contact->name = 'A new name'; $contact->setEmail($anotherValidEmail); $contact->save(); // This doesn't work! Not even the name field is updated
I tried using update() instead of save() .
I also tried using $contact->email = $newEmailObject , but I get the same results.
Has anyone encountered this issue? What am I doing wrong?
I am using Mac OS X , PhalconPHP 1.2.3
I also tested it on Windows 8, with PhalconPHP 1.1.0
Any help is appreciated.
updated
I posted the results of $ contact-> getMessages () after saving (), but I am not getting any results. As if save () was successful, however the SQL statement is not executed.
LAST UPDATE: Problem is replicated and workaround found!
We were able to reproduce the problem. We doing this:
$contact = Contact::findFirst(123); if ($contact->email->emailaddress != $newaddress) { $email = new Email(); $email->emailaddress = $newaddress; $contact->email = $email; $contact->save(); }
This does not work! As soon as we compare the emailaddress field with the email-related object, the line for saving the new address does not work.
However, if we change the code a bit and do this:
$contact = Contact::findFirst(123); if ($contact->email->emailaddress != $newaddress) { $email = new Email(); $email->emailaddress = $newaddress;
It really works.
I assume that loading a linked object affects the ability to update this particular field and again find a contact object, clears the linked object.
So, we have a workaround that I hope will help other people who are faced with the same problem.