Updating Related Records With Doctrine Hooks

Posted on February 2010

On a project that I am currently working on at work, a unique problem presented itself.

Take into account the following relationship:

UML Diagram of Users, Profiles, and Activity Logs

The above diagram is incomplete, but signifies the problem we are seeking to solve. The steps field in Profile is an aggregate of the steps that exist in each activity log related to a profile.

While this could have been designed to dynamically calculate this field, given the scope of the application, number of users, and the critical nature that steps present to ranking user profiles, it was decided that steps become a field that would be updated manually when it needed to change.

Because of this we found a small problem:what happens when a user updates an activity log in such a way, that it changes the value of steps.

Thanks to some handy Doctrine Magic, the following snippet provides our solution: postInsert and postUpdate hooks in the ActivityLog model that update the steps field in the related Profile.