title: Doctrine2 + Symfony2 Keeping history on every insert, update, delete
body: <p>I'm developing web application. The most important feature should be history(versioning) and approving inserted data by user. It means that user can see all history changes, use old version again, validate or refuse a new version, which is inserted by user without permision.</p>
  
  <p>I have one table actionHistory(date,user,action,isValid) then I have e.g. Person, Address, CustomField and CustomValue tables. Person manyToMany Address. Person oneToMany CustomFields.
  CustomFields oneToOne CustomValue. Every table also have a historical table.</p>
  
  <p>E.G. When I am persisting person with custom field-value. I have to create person,customfield-value history tables. Copy this versions into them. Connect all historical tables and also connect with <code>actionHistory</code>.</p>
  
  <p>At the beginning I've used EventListener and Lifecycle Event (postPersist,postDelete,postUpdate), but the main problem is that when I would like to add previous example I have always problem to connect these tables. I don't know which entity will persist first and when I try the second entity doesn't exist yet,etc.</p>
  
  <pre><code>public function postUpdate(LifecycleEventArgs $args)
  {
      $entity = $args-&gt;getEntity();
      $em = $args-&gt;getEntityManager();
  
      if ($entity instanceof Address) {
          $this-&gt;addPersonHistory($em, $entity);
          $this-&gt;saveAddressHistory("UPDATE", $em, $entity, "Address");
      }
      if ($entity instanceof CustomField) {
          $this-&gt;addCustomFieldHistory($em, $entity);
          $this-&gt;saveCustomFieldHistory("UPDATE", $em, $entity, "CustomField");
      }
      if ($entity instanceof CustomValue) {
          $this-&gt;saveCustomValueHistory("UPDATE", $em, $entity, "CustomValue");
      }
      if ($entity instanceof Person) {
          $this-&gt;savePersonHistory("UPDATE", $em, $entity, "Person");
      }
  }
  </code></pre>
  
  <p>Is there some better solution for this situation? Because I am stuck now, when I have to face form adding of person-customField-customValue.</p>
  
  <p>Thank you in advance!</p>