I’m struggling with the following, in a entity class I have a preUpdate lifeCycleCallback which has to persist a new entity before it flushes the changes for a auditTrail.
In preRemove and prePersist this works perfectly but in preUpdate nothing happends. If I call flush myself it goes in a recursive loop.
According to the Google groups for doctrine-user putting it in onFlush should be a option but in that event I can’t access the old values of the entity to save this old values in a new other entity for the audittrail.
Some small example what i’m trying to archive:
<?php
/**
* @Entity
* @HasLifeCycleCallbacks
*/
class someEntity {
... annotations ...
/**
* @PreUpdate
*/
public function addAuditTrail() {
$em = \Zend_Registry::get('doctrine')->getEntityManager();
$entity = new AuditTrail();
$entity->action = 'update';
$entity->someField = $this->someField;
$em->persist($entity); //this just doesn't do anything :-(
}
}
?>
It’s not real code, just something to illustrate you what I want. I also tried something like this:
$em->getUnitOfWork()->computeChangeSet($em->getClassMetaData(get_class($entity)), $entity);
Which should work according to this topic: http://groups.google.com/group/doctrine-user/browse_thread/thread/bd9195f04857dcd4
If I call the flush again but that causes Apache to crash because of some infinite loop.
Anyone who got ideas for me? Thanks!
You should never use the entitymanager inside your entities. If you would like to add audit trails, you should map the “SomeEntity” entity to the “AuditTrail” entity and do something like
If you set the cascade option on the mapping, it will get persisted when you persist “SomeEntity”.