cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

save() fails to update (silently) when a related object is loaded

Open hlasso opened this issue 12 years ago • 14 comments

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.

hlasso avatar Apr 04 '14 22:04 hlasso

I have similar issue #2320, I'll try try your workaround.

mruz avatar Oct 11 '14 07:10 mruz

Not work for me. Sometimes updates and sometimes not.

mruz avatar Oct 23 '14 16:10 mruz

@dreamsxin could you look at this?

mruz avatar Oct 25 '14 14:10 mruz

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 avatar Oct 26 '14 23:10 dreamsxin

@dreamsxin check in the database if data was updated.

mruz avatar Oct 27 '14 05:10 mruz

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);

dreamsxin avatar Oct 27 '14 06:10 dreamsxin

@hlasso

I have just comment here

duythien avatar Oct 27 '14 06:10 duythien

@duythien but in the Contact model there is belongsTo relation with alias Email so getEmail() is unnecessary.

mruz avatar Oct 27 '14 06:10 mruz

@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)
}

mruz avatar Oct 27 '14 06:10 mruz

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 avatar Oct 27 '14 07:10 dreamsxin

@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.

mruz avatar Oct 28 '14 07:10 mruz

I change db to mysql test again.

dreamsxin avatar Oct 28 '14 08:10 dreamsxin

@dreamsxin Any news?

sergeyklay avatar Jun 08 '17 19:06 sergeyklay

@sergeyklay It should be closed.

dreamsxin avatar Jun 08 '17 23:06 dreamsxin