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

Uneditable entries, problem with relations

I would like to have uneditable entries in my project. This means, based on "editable" field, or other conditions entry may not be updated. I solved this using beforeValidation and beforeDelete, returning false and appending an error message. However this caused problems, when I had those entries as related entities.

E.g. I have some objects, lets say music genres. User can create/update/delete genres, but some of them are default and can not be edited. Now I want to save a song with this genre and I can't, because Phalcon implicitly saves all related entities, and default genres are uneditable.

I could not find any way to check if object has been modified. There are some DIRTY_.. states, but it's unclear what do they actually do.

Does anyone have an idea how can this be achieved without breaking relations?



98.9k
Accepted
answer

You can use the method Phalcon\Mvc\Model::hasChanged to check if a record has been changed with respect the one in the database. You can also activate the dynamic update feature to avoid updating records that haven't changed.