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:

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

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? :)



46.6k

I believe that you have to turn on dynamic updates and also snapshots to stop the cascade updates. Also, I think that you have to check that no changes were made as well. I'm trying to find my research into that but I guess that it was lost in the code shuffle.