Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

What is the cleanest way to prevent a model field from ever being updated?

Lets say that I don't want a UUID field to be updatable: Should I check for ->hasChanged('uuid') in the beforeUpdate event and cancel if it is changed? Is there a cleaner way to do it, perhaps in the initialize method?



3.8k
Accepted
answer
edited Jan '15

This? http://docs.phalconphp.com/en/latest/reference/models.html#skipping-columns

Or the beforeUpdate() event in a model class.

public function beforeUpdate()
{       
    // If password field is empty, ignore it
    if (empty($this->password)) {
        $this->skipAttributesOnUpdate(array('password'));
    }
}


46.8k

Sure. I like it because I can add it to the initalize function, which I keep at the bottom of the model.



19.6k

I should do $this->skipAttributesOnUpdate(["password"]); of Edward in a initialize function if you are sure you NEVER want to get updated.