save() fails to update (silently) when a related object is loaded
When updating a related object we have found that first accesing the related object's attributes does not allow to update the relation.
We have a couple of classes:
class Contact extends \Phalcon\Mvc\Model
{
public function initialize()
{
$this->belongsTo('email_id', 'Email', 'id', ['alias' => 'Email']);
}
}
class Email extends \Phalcon\Mvc\Model
{
public function initialize()
{
$this->hasMany('id', 'Contact', 'email_id', ['alias' => 'Contacts']);
}
}
If we have valid contact and want to replace the email related object with a new object the system works:
$str = '[email protected]'; // New address
$contact = Contact::findFirst(123); // 123 is a valid ID
$newemail = new Email();
$newemail->emailaddress = $str;
$contact->email = $newemail;
$contact->save();
However, if we first validate that we are not updating to the same email...
$str = '[email protected]'; // New address
$contact = Contact::findFirst(123); // 123 is a valid ID
if ($contact->email->emailaddress != $str) {
$newemail = new Email();
$newemail->emailaddress = $str;
$contact->email = $newemail;
$contact->save();
}
This in fact does not work... SAVE() is called, the new Email object (and record) is created... but Contact's relation to email is not updated to reflect the change.
We started a thread a few months ago on Stackoverflow, but until today we were able to replicate the issue and found a workaround. http://stackoverflow.com/questions/18346569/phalconphp-framework-how-to-update-a-model-object-and-the-related-object
I don't know if this is a bug, an undocumented feature or an expected outcome, so decided to report it.
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
I have similar issue #2320, I'll try try your workaround.
Not work for me. Sometimes updates and sometimes not.
@dreamsxin could you look at this?
I test like this ok:
class News extends \Phalcon\Mvc\Model {
public function initialize() {
$this->useDynamicUpdate(true);
$this->belongsTo('user_id', 'Users', 'id');
}
}
class Users extends \Phalcon\Mvc\Model {
public function initialize()
{
$this->hasMany('id', 'News', 'user_id');
}
}
$user = new Users;
$user->username = 'test';
$news = News::findFirst(1);
$news->Users = $user;
$ret = $news->save();
var_dump($ret, $news->user_id, $user->id);
My database is Postgresql.
@dreamsxin check in the database if data was updated.
I run many times, user_id has changed
$user = new Users;
$user->username = 'test';
$news = News::findFirst(1);
var_dump($news->user_id);
$news->Users = $user;
$ret = $news->save();
var_dump($ret, $news->user_id, $user->id);
@duythien but in the Contact model there is belongsTo relation with alias Email so getEmail() is unnecessary.
@dreamsxin you have to save user after load related object
$user = new Users;
$user->username = 'test';
$news = News::findFirst(1);
$user->news = $news;
$ret = $user->save(); //return true, but not save
Could you test with the same example like @hlasso
$str = '[email protected]'; // New address
$contact = Contact::findFirst(123); // 123 is a valid ID
$newemail = new Email();
$newemail->emailaddress = $str;
$contact->email = $newemail;
$contact->save();
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.
I have the same problem with:
$id = 1;
$post = new Posts();
$post->content = "test";
$post->user_id = $id;
if ($post->create() === true) {
// Increment posts counter for the user
$user = $post->getUser();
++$user->posts;
$user->save(); // no errors, looks like successful, however no execute SQL (sometimes works and sometimes it does not, strange)
}
I get, test it ok. @mruz
class News extends \Phalcon\Mvc\Model {
public function initialize() {
$this->useDynamicUpdate(true);
$this->belongsTo('user_id', 'Users', 'id');
}
}
class Users extends \Phalcon\Mvc\Model {
public function initialize()
{
$this->hasMany('id', 'News', 'user_id');
}
}
$news = News::findFirst(1);
$user = $news->getUsers();
var_dump($news->user_id, $user->sex);
++$user->sex;
$user->save();
var_dump($news->user_id, $user->sex);
@dreamsxin The problem occurs when I'm trying to save one model and after that second:
$logged_id = 1;
$news = new News();
$news->user_id = $logged_id;
$news->content = "test";
if ($news->create() === true) {
$user = $news->getUser();
var_dump($news->user_id, $user->sex);
++$user->sex;
$user->save();
var_dump($news->user_id, $user->sex);
// The values seems OK, but if I run
$db = \Phalcon\DI::getDefault()->getShared('db');
$test = $db->fetchOne('SELECT * FROM `users` WHERE `id` = ?',
\Phalcon\Db::FETCH_ASSOC, [$logged_id]);
// The value of `sex` attribute is not changed
var_dump($test['sex']);
}
For me it works unpredictable, because sometimes works, sometimes not.
I change db to mysql test again.
@dreamsxin Any news?
@sergeyklay It should be closed.