Symfony2 Troubleshooting - Lifecycle callbacks not being called when using (docblock) annotations

Issue:

Lifecycle callbacks such as prepersist and preupdate are not being called when defined as annotations in the entity class.


Systems Invovled:

Symfony2, Doctrine


Solutions and/or Notes:

Solution #1: Make sure that you do NOT have any YAML files in /project/src/BSL/ProjectBundle/Resources/config/doctrine.  If there are YAML files, either remove the entire "doctrine" directory or else remove all the YAML files contained within that directory.  Load your data fixtures and then check the database to see if any fields who's value should be coming from a lifecycle callback is present.  If an expect value is not there, proceed to solution #2 below.

Solution #2: Make sure you have @HasLifecycleCallbacks on the entity-class PHP DocBlock.  Code should look similar:

  1. /**
  2.  * @Entity
  3.  * @HasLifecycleCallbacks
  4.  */

It if VERY important that if you're using annotation instead of XML or YAML for ORM metadata that @HasLifecycleCallbacks is present or else Doctrine will just ignore any lifecycle callbacks that you may have.

Note: It would seem that having both annotation and YAML for ORM metadata will cause conflicts.  YAML data will be used over annotation data so if you're using annotation for ORM metadata and YAML for everything else, them make sure solution #1 is followed.