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

Strange behaviour when saving

Hi! I have two tables: Tasks and Users. The table Tasks has foreign key to the table Users. In the model Users I set logging in the function beforeUpdate error_log('Users::beforeupdate'). I got a few tasks using Tasks::find($condition), changed nodes and saved each $task->save(). And function beforeUpdate in the model Users was executed! WHY??? This behaviour can make an error which is too difficult to find: first user can open a task and work with it during long time; at this time other user can change node User, but these changes will be rewritten when first user save his data. But when I get the task from table using Tasks::findFirst($condition) the function beforeUpdate in the model Users does not execute... Please, explain to me: described behavior is normal or wrong?



51.2k

First of all check how your foreign keys are set in DB. You probably have set "on update cascade". Also, in model relations, it is a normal behavior that the "beforeUpdate" method is called. Second, beforeUpdate method is not executed because ... find() does not execute any updates.



3.0k

You didn't understand me. I know that the method find() does not execute any updates. I update each task using $task->save(). But I got these tasks using method find(). In this case the method beforeUpdate in the model Users is carried out also.