title: Symfony2: PrePersist/PreUpdate lifecycle-event not fired
body: <p>Two Entities <strong>GalleryAlbum</strong> and <strong>GalleryImage</strong> have OneToMany/ManyToOne relationship:</p>
  
  <p><code>One GalleryAlbum ==== can have ====&gt; Many GalleryImage</code></p>
  
  <p><code>Many GalleryImage === can be in ===&gt; One GalleryAlbum</code></p>
  
  <p>(sources below)</p>
  
  <p><strong>What is the problem?</strong></p>
  
  <ol>
  <li><p>Adding (uploading) files to GalleryAlbum</p></li>
  <li><p>$em->persist($album)</p></li>
  <li><p>$em->flush()</p></li>
  <li><p>For each uploaded file GalleryAlbum class creates and adds to $images a new GalleryImage entity</p></li>
  <li><p>My ECHO/EXIT test is not shown (GalleryImage's prePersist/preUpdate event callback function named preUpload is not triggered!)</p></li>
  <li><p>My new images are not saved to the database? Why?</p></li>
  </ol>
  
  <p><strong>What is weird!</strong> <em>If I do:</em></p>
  
  <ol>
  <li><p>Adding (uploading) files</p></li>
  <li><p>$em->persist($album)</p></li>
  <li><p>$em->flush()</p></li>
  <li><p><strong>again $em->flush()</strong></p></li>
  <li><p>My ECHO/EXIT test is shown (GalleryImage's prePersist/preUpdate event callback function named preUpload is triggered!)</p></li>
  <li><p>(if i delete echo/exit) My new GalleryImages are saved now!!!</p></li>
  </ol>
  
  <p><strong>Why?</strong></p>
  
  <p>Why is preUpload never triggered when I flush() once, and is triggered when I flush() twice?</p>
  
  <p><em># src GalleryAlbum.php</em></p>
  
  <pre>
      <i>/**
       * @ORM\Entity
       * @ORM\HasLifecycleCallbacks
       * @ORM\Table(name="gallery_album")
       */</i>
      class <strong>GalleryAlbum</strong>
      {
          // some properties like <em>id, name, description, etc</em>
  
          <i>/**
           * @ORM\OneToMany(targetEntity="GalleryImage", mappedBy="parent")
           */</i>
          protected <b>$images</b>;
  
          <i>/* Files container. Used for upload service. Must not be persisted. */</i>
  
          protected <b>$files</b>;    
  
          <i>/* @ORM\Column(type="boolean", nullable=TRUE)
           *
           * if set to true will updateing object and calling preUpdate event callback
           * becouse it's always set to null in database by prePersist event callback */</i>
  
          protected <b>$files_added</b>;
  
          <i>/**
           * Set container files
           * 
           * @return GalleryAlbum
           */</i>
           <b>public function</b> setFiles(<b>$files</b>)
           {
               <b>$this</b>->files = <b>$files</b>;
               <b>$this</b>->files_added = <b>true</b>;
               <i>/* setting files_added to true forces EntityManager to update 
                * this GalleryAlbum even if no other properties have changed */</i>
  
               return <b>$this</b>;
           }
  
          <i>/**
           * @ORM\PrePersist()
           * @ORM\PreUpdate()
           */</i>
          <b>public function</b> preUpload()
          {
              if(<b>null</b> !== <b>$this</b>->files) {
                  foreach(<b>$this</b>->files as <b>$key</b> => <b>$file</b>) {
                    <b>$this</b>->addGalleryElement(<b>$file</b>);
                    unset(<b>$this</b>->files[<b>$key</b>]);
                  }
              }
              <i>/* Resetting property files_added to NULL 
               * so it always stays null in database */</i>
              <b>$this</b>->files_added = null;
          }
  
  
          <i>/**
           * Constructing new GalleryImage and setting it's file and parent
           */</i>
          <b>public function</b> addGalleryElement(<b>$file</b>)
          {      
              <b>$element</b> = <b>new</b> GalleryImage(<b>$this</b>, <b>$file</b>);
              <b>$this</b>->addGalleryImage(<b>$element</b>);
          }
      }
  </pre>
  
  <p><em># src GalleryImage.php</em></p>
  
  <pre>
      <i>/**
       * @ORM\Entity
       * @ORM\HasLifecycleCallbacks
       * @ORM\Table(name="gallery_image")
       */</i>
      class <strong>GalleryImage</strong>
      {
          // some properties like <em>id, name, description, etc</em>
  
          <i>/**
           * @ORM\ManyToOne(targetEntity="GalleryAlbum", inversedBy="images")
           * @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
           */</i>
          protected <b>$parent</b>;
  
          <i>/* Constructing new GalleryImage */</i> 
  
          <b>public function</b> __construct(<b>$parent</b> = <b>null</b>, <b>$file</b> = <b>null</b>)
          {
              if(<b>$parent</b>) <b>$this</b>->setParent(<b>$parent</b>);
              if(<b>$file</b>) <b>$this</b>->setFile(<b>$file</b>);     
          }
  
          <i>/**
           * @ORM\PrePersist()
           * @ORM\PreUpdate()
           */</i>
          <b>public function</b> preUpload()
          {
              <b>echo</b> 'TEST: is this event callback function fired?'; <b>exit</b>;
  
              if(<b>null</b> !== <b>$this</b>->file) {
                  <b>$this</b>->path = <b>$this</b>->file->guessExtension();
              }
  
              <b>$this</b>->file_added = <b>null</b>;
          }
      }
  </pre>