Events ====== Doctrine PHPCR-ODM features a lightweight event system that is part of the Common package. For a general introduction, see the corresponding chapter in the `Doctrine ORM documentation `_ Lifecycle Events ---------------- The DocumentManager and PHPCR-ODM UnitOfWork trigger a bunch of events during the life-time of their registered documents. - prePersist - occurs before a new document is created in the repository - postPersist - occurs after a document has been created in repository. generated fields will be available in this state. - preUpdate - occurs before an existing document is updated in the repository, during the flush operation - postUpdate - occurs after an existing document has successfully been updated in the repository - postLoad - occurs after the document has been loaded from the repository - preMove - occurs before a document move operation is persisted to the PHPCR session during flush - postMove - occurs after a document move operation has been persisted to the PHPCR session during flush - preRemove - occurs before a document is removed from the repository - postRemove - occurs after the document has been successfully removed from the repository - preFlush - occurs at the very beginning of a flush operation. This event is not a lifecycle callback. - onFlush - occurs after the change-sets of all managed documents have been computed. This event is not a lifecycle callback. - postFlush - occurs at the end of a flush operation. This event is not a lifecycle callback. - onClear - occurs when the DocumentManager#clear() operation is invoked, after all references to documents have been removed from the unit of work. This event is not a lifecycle callback. - loadClassMetadata - occurs after mapping metadata for a class has been loaded from a mapping source (annotations/xml/yaml). .. note:: If you use PHPCR-ODM inside Symfony2, you can use the tag doctrine_phpcr.event_listener to register a service as event listener. See the `Documentation of DoctrinePHPCRBundle `_ for more information. .. warning:: Note that the postLoad event occurs for a document before any associations have been initialized. Therefore it is not safe to access associations in a postLoad callback or event handler. You can access the Event constants from the ``Event`` class in the PHPCR-ODM package. .. code-block:: php createdAt = date('Y-m-d H:m:s'); } /** @PrePersist */ public function doOtherStuffOnPrePersist() { $this->value = 'changed from prePersist callback!'; } /** @PostPersist */ public function doStuffOnPostPersist() { $this->value = 'changed from postPersist callback!'; } /** @PostLoad */ public function doStuffOnPostLoad() { $this->value = 'changed from postLoad callback!'; } /** @PreUpdate */ public function doStuffOnPreUpdate() { $this->value = 'changed from preUpdate callback!'; } .. code-block:: yaml MyPersistentClass: lifecycleCallbacks: prePersist: [ doStuffOnPrePersist, doOtherStuffOnPrePersistToo ] postPersist: [ doStuffOnPostPersist ] .. code-block:: xml The methods mapped to the callbacks in xml or yml need to be public methods of your document. The ``key`` of the lifecycleCallbacks is the name of the method and the value is the event type. The allowed event types are the ones listed in the previous Lifecycle Events section. .. note:: Contrary to the ORM, PHPCR-ODM does **not** use the @HasLifecycleCallbacks marker. Listening to Lifecycle Events ----------------------------- This works exactly the same as with the `ORM events `_.