title: Symfony2 : Image Modification with doctrine lifcycle functions
body: <p>Hellow Everybody</p>
  
  <p>i'm new in symfony2 world, i'm working on a e-commerce project and i need to modifiy the image of a product, i use doctrine life cycle functions to upload the image and liipimaginebundle to handle to thmbnails. my code is </p>
  
  <p>Article.php</p>
  
  <pre><code>&lt;?php
  
  namespace StoreBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Symfony\Component\validator\Constraints as Assert;
  
  /**
   * Image
   *
   * @ORM\Table(name="image")
   * @ORM\Entity(repositoryClass="StoreBundle\Repository\ImageRepository")
   * @ORM\HasLifecycleCallbacks
   */
  class Image
  {
      /**
       * @var int
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @var \DateTime
       * 
       * @ORM\Column(name="updated_at",type="datetime", nullable=true) 
       */
      private $updateAt;
  
      /**
       * @ORM\PostLoad()
       */
      public function postLoad()
      {
          $this-&gt;updateAt = new \DateTime();
      }
  
      /**
       * @ORM\Column(type="string",length=255, nullable=true) 
       */
      private $path;
  
  
      public $file;   
  
      public function getUploadRootDir()
      {
          return __dir__.'/../../../public_html/uploads';
      }
  
      public function getAbsolutePath()
      {
          return null === $this-&gt;path ? null : $this-&gt;getUploadRootDir().'/'.$this-&gt;path;
      }
  
      public function getAssetPath()
      {
          return 'uploads/'.$this-&gt;path;
      }
  
      /**
       * @ORM\PrePersist()
       * @ORM\PreUpdate() 
       */
      public function preUpload()
      {
          $this-&gt;tempFile = $this-&gt;getAbsolutePath();
          $this-&gt;oldFile = $this-&gt;getPath();
          $this-&gt;updateAt = new \DateTime();
  
          if (null !== $this-&gt;file)
              $this-&gt;path = sha1(uniqid(mt_rand(),true)).'.'.$this-&gt;file-&gt;guessExtension();
      }
  
      /**
       * @ORM\PostPersist()
       * @ORM\PostUpdate() 
       */
      public function upload()
      {
          if (null !== $this-&gt;file) {
              $this-&gt;file-&gt;move($this-&gt;getUploadRootDir(),$this-&gt;path);
              unset($this-&gt;file);
  
              if ($this-&gt;oldFile != null) unlink($this-&gt;tempFile);
          }
      }
  
  
      /**
       * @ORM\PreRemove() 
       */
      public function preRemoveUpload()
      {
          $this-&gt;tempFile = $this-&gt;getAbsolutePath();
      }
  
      /**
       * @ORM\PostRemove() 
       */
      public function removeUpload()
      {
          if (file_exists($this-&gt;tempFile)) unlink($this-&gt;tempFile);
      }
  
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set updateAt
       *
       * @param \DateTime $updateAt
       * @return Image
       */
      public function setUpdateAt($updateAt)
      {
          $this-&gt;updateAt = $updateAt;
  
          return $this;
      }
  
      /**
       * Get updateAt
       *
       * @return \DateTime 
       */
      public function getUpdateAt()
      {
          return $this-&gt;updateAt;
      }
  
      /**
       * Set path
       *
       * @param string $path
       * @return Image
       */
      public function setPath($path)
      {
          $this-&gt;path = $path;
  
          return $this;
      }
  
      /**
       * Get path
       *
       * @return string 
       */
      public function getPath()
      {
          return $this-&gt;path;
      }
  
  }
  </code></pre>
  
  <p>ArticleImageType.php</p>
  
  <pre><code>&lt;?php
  
  namespace StoreBundle\Form;
  
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolver;
  
  use StoreBundle\Form\ImageType;
  use Symfony\Component\Form\Extension\Core\Type\FileType;
  
  class ArticleImageType extends AbstractType
  {
      /**
       * @param FormBuilderInterface $builder
       * @param array $options
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('image', new ImageType())
          ;
      }
  
      /**
       * @param OptionsResolver $resolver
       */
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'StoreBundle\Entity\Article'
          ));
      }
  }
  </code></pre>
  
  <p>And in my controller i put : </p>
  
  <pre><code>public function imageEditAction(Request $request, Article $article)
      {
          $editForm = $this-&gt;createForm('StoreBundle\Form\ArticleImageType', $article);
          $editForm-&gt;handleRequest($request);
  
          if ($editForm-&gt;isSubmitted() &amp;&amp; $editForm-&gt;isValid()) {
              $em = $this-&gt;getDoctrine()-&gt;getManager();
              $em-&gt;persist($article);
              $em-&gt;flush();
  
              return $this-&gt;redirectToRoute('admin_article_show', array('id' =&gt; $article-&gt;getId()));
          }
  
          return $this-&gt;render('AdminBundle:article:imageEdit.html.twig', array(
              'article' =&gt; $article,
              'edit_form' =&gt; $editForm-&gt;createView(),
          ));
      }
  </code></pre>
  
  <p>when i try modifying the image. is doesn't modified, and the old image is appears in the article. i need your help please and thanks in advance</p>