This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

vdomah
vdomah

How not to not override related model's records when you save a form in backend?

Created a widget to display/add/remove some related records, lets assume comments for post. When submitting the form comments that are already added to post getting detached from post. I've tried to capture the assignment/detaching of the comments in the saving, saved, updated etc static methods - didn't help. While execution of that methods comments are still attached to post.

Only solution which helped somehow - do exit; at the end of the method ::saved after checking that this will happen only for the backend: if (! $ this-> app-> runningInBackend ()) return;

PostModel::saved(function($model) {
    if (!$this->app->runningInBackend())
        return;

    $m = PostModel::find($model->id);

    foreach ($m->comments as $comment) {
        $comment->post_id = $m->id;
        $comment->save();
    }

    //return true;
    exit;
});

But it breaks the flow of model events so need to find the place where recodrs are detached.

1-1 of 1

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.