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

How to disable saving related records

I have the following models and relations (sample code) (phalcon 1.3.5)

<?php
class User extends \Phalcon\Mvc\Model
{
    public function initialize()
    {
        parent::initialize();
        $this->hasMany("user_id", "UserContent", "user_id", ['alias' => 'user']);
    }
}
<?php
class UserContent extends \Phalcon\Mvc\Model
{
    public function initialize()
    {
      parent::initialize();
      $this->belongsTo("user_id","User","user_id", ["alias" => "user"] );
    }
}
<?php
$user_content = new UserContent();
$user_content->some_key = 'some_value';
$user_content->save();

Calling save() on the UserContent is triggering a save on the related 'User' module as well. I have gone through phalcon code and found that save() calls the _preSaveRelatedRecords() function.

Is there a way to disable this functionality or a work around tonot save User when UserContent is saved.

edited May '15

Is this your only code for UserContent?

<?php
$user_content = new UserContent();
$user_content->some_key = 'some_value';
$user_content->save();

I'm not 100% sure but unless you set $user_content->user (and in that case if the model is using dynamic update and has not changed) belongs-to relations are not saved

@Oscar : That's not the only code and yes, $user_content->user is being accessed.

Thanks for the answer, I'll try to use dynamic update.

edited Feb '17

This must be used only when the primary key is updated. Like in the database relations. Not for saving the related records and fields.

Is this correct?

I think there is no way to stop saving related records right now. I am going to try and add reset of the related records in order to save only the main model. After that you can always call again the relation.

My current sulution is to call a clear model instance and save new fields before calling other relations.

https://docs.phalcon.io/en/latest/reference/models.html#cascade-restrict-actions

https://docs.phalcon.io/en/latest/api/Phalcon_Mvc_Model_Relation.html

Relation::NO_ACTION