We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Disable saving of related records

Hi

Having a few issues with the ORM executing unnessary queries which I can't find a way to disable. I'd much rather explicitly call save on models than have the ORM try do it's magic with certian relations.

Here is an example I have created in its simplest form, not actually tested this code, it's more to explain my problem.

Models:

// User
class User extends Model
{
    public $userId;
    public $countryId;
    public $email;
    public $createdAt;
    public $updatedAt;

    public function initialize()
    {
        $this->setSource('users');
        $this->useDynamicUpdate(true);

        $this->hasOne('countryId', 'Country', 'countryId', [
            'alias' => 'country'
        ]);
    }

    public function beforeValidationOnCreate()
    {
        $this->createdAt = date('Y-m-d H:i:s');
    }

    public function beforeValidationOnUpdate()
    {
        $this->updatedAt = date('Y-m-d H:i:s');
    }
}

// Country
class Country extends Model
{
    public $countryId;
    public $name;
    public $createdAt;
    public $updatedAt;

    public function initialize()
    {
        $this->setSource('users');
        $this->useDynamicUpdate(true);
    }

    public function beforeValidationOnCreate()
    {
        $this->createdAt = date('Y-m-d H:i:s');
    }

    public function beforeValidationOnUpdate()
    {
        $this->updatedAt = date('Y-m-d H:i:s');
    }
}

Code to create the unnessary update.

$user = User::findFirstByEmail('[email protected]');
var_dump($user->country);

$user->email = '[email protected]';
$user->save();

This will result in the following two queries (ignoring the select).

UPDATE `users` SET `email` = '[email protected]', `updatedAt` = '2015-02-21 18:24:07' WHERE `userId` = '1'
UPDATE `countries` SET `updatedAt` = '2015-02-21 18:24:07' WHERE `countryId` = '1'

I have played around with the 'action' paramater on the relation passing constants from Phalcon\Mvc\Model\Relation but no change.

Any suggestions? :)



2.1k

the before validation on update calls is handled before the save. so i am pretty sure thats why it changes =d

For the country model though, updated and created is useless? since country list is pretty much supposed to be static, and unless drastic changes in real world u will always have a fix number of country =d

Country is just an example, wanted to keep it simple for my explination.

There must be a way to disable saving of related records, if not that's really a huge oversight.



2.1k

one thing i know of for sure is, unless you retrieve the related records. it wont get saved.

If you load model a, which is related to model b, but do not call it. by any means, $a->b or $a->getB(). b model will not be loaded into existence and it wont be saved. when you call $a->save();

The very moment u access any thing from model b, a will have the related model b and thus it will go thru the normal saving route.

This is indeed stange.

I am having deadlock problems caused by this. It could have a way to disable update related.

edited Mar '17

one thing i know of for sure is, unless you retrieve the related records. it wont get saved.

I have the same situation in the phalcon 3.0.0. Update to phalcon 3.1.1 But if I use code like this

public function afterFetch()
    {
        // Преобразуем данные
        $this->data = json_decode($this->data, JSON_OBJECT_AS_ARRAY);
    }

then the model is considered changed. It is not comfortable.