title: Creating preUpdate or preFlush event with Event Listener
body: <p>I have a onFlush() event which works fine but what I need to do is to turn that into preFlush() or preUpdate() both acceptable. I did preFlush() but for some reason it doesn't do anything. Not even an error. What am I missing?</p>
  
  <p><strong>TEST:</strong> I placed <code>exit</code> in <code>preFlush()</code> to see if it is being called at all or not. Outcome is: <strong>1</strong> so <code>foreach()</code> is never run! It is an empty array. I also tested preUpdate() and all the lines in that get runed but no data inserted.</p>
  
  <pre><code>public function preFlush(PreFlushEventArgs $args)
  {
      $em = $args-&gt;getEntityManager();
      $uow = $em-&gt;getUnitOfWork();
      echo '1';
      foreach ($uow-&gt;getScheduledEntityUpdates() as $entity) {
          echo '2';
          if ($entity instanceof User) {
              echo '3';
          }
      }
      exit;
  }
  </code></pre>
  
  <p>I created them after reading the <a href="http://doctrine-orm.readthedocs.org/en/latest/reference/events.html" rel="noreferrer">documentation</a>.</p>
  
  <p><strong>service.yml</strong></p>
  
  <pre><code>services:
      entity.event_listener.user:
          class:  Site\FrontBundle\EventListener\Entity\UserListener
          tags:
              - { name: doctrine.event_listener, event: preUpdate }
              - { name: doctrine.event_listener, event: onFlush }
              - { name: doctrine.event_listener, event: preFlush }
  </code></pre>
  
  <p><strong>Working onFlush() example:</strong></p>
  
  <pre><code>class UserListener
  {
      public function onFlush(OnFlushEventArgs $args)
      {
          $em = $args-&gt;getEntityManager();
          $uow = $em-&gt;getUnitOfWork();
  
          foreach ($uow-&gt;getScheduledEntityUpdates() as $entity) {
              if ($entity instanceof User) {
                  $userLog = new UserLog();
                  $userLog-&gt;setDescription($entity-&gt;getId() . ' being updated.');
  
                  $em-&gt;persist($userLog);
  
                  // Instead of $em-&gt;flush() cos we're already in flush process
                  $userLogMetadata = $em-&gt;getClassMetadata(get_class($userLog));
                  $uow-&gt;computeChangeSet($userLogMetadata, $userLog);
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>Not working preFlush() example:</strong></p>
  
  <pre><code>class UserListener
  {
      public function preFlush(PreFlushEventArgs $args)
      {
          $em = $args-&gt;getEntityManager();
          $uow = $em-&gt;getUnitOfWork();
  
          foreach ($uow-&gt;getScheduledEntityUpdates() as $entity) {
              if ($entity instanceof User) {
                  $userLog = new UserLog();
                  $userLog-&gt;setDescription($entity-&gt;getId() . ' being updated.');
  
                  $em-&gt;persist($userLog);
  
                  // Instead of $em-&gt;flush() cos we're already in flush process
                  $userLogMetadata = $em-&gt;getClassMetadata(get_class($userLog));
                  $uow-&gt;computeChangeSet($userLogMetadata, $userLog);
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>Not working preUpdate() example</strong></p>
  
  <pre><code>class UserListener
  {
      public function preUpdate(LifecycleEventArgs $args)
      {
          $entity = $args-&gt;getEntity();
          $em = $args-&gt;getEntityManager();
          $uow = $em-&gt;getUnitOfWork();
  
          if ($entity instanceof User) {
              $userLog = new UserLog();
              $userLog-&gt;setDescription($entity-&gt;getId() . ') been updated.');
  
              $em = $args-&gt;getEntityManager();
              $em-&gt;persist($userLog);
              $userLogMetadata = $em-&gt;getClassMetadata(get_class($userLog));
              $uow-&gt;computeChangeSet($userLogMetadata, $userLog);
          }
      }
  }
  </code></pre>