title: Symfony forms. File upload
body: <p>Trying to manage file upload with Entity, but i get this error:</p>
  
  <blockquote>
    <p>Fatal error: Call to a member function move() on a non-object in /home/projectpath/src/BS/MyBundle/Entity/Items.php on line 327 Call Stack: 0.0002 333264 1. {main}() /home/projectpath/web/app_dev.php:0 0.0450 1158160...</p>
  </blockquote>
  
  <p>Here's the entity class:</p>
  
  <pre><code>namespace BS\BackstretcherBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Symfony\Component\HttpFoundation\File\UploadedFile;
  use Symfony\Component\Validator\Constraints as Assert;
  
  /**
  * MB\MyBundle\Entity\Items
  *
  * @ORM\Table(name="items")
  * @ORM\Entity
  * @ORM\HasLifecycleCallbacks
  */
  class Items
  {
      private $filenameForRemove;
  
      /**
       * @Assert\File(maxSize="60000000")
       */
      public $file;
      ...
  
      protected function getUploadDir()
      {
         return 'images/items/';
      }
  
      protected function getUploadRootDir()
      {
        return __DIR__.'/../../../../web/'.$this-&gt;getUploadDir();
      }
  
      public function getWebPath()
      {
        return null === $this-&gt;file ? null : $this-&gt;getUploadDir().'/'.$this-&gt;getNameEn();
      }
  
      public function getAbsolutePath()
      {
        return null === $this-&gt;file ? null : $this-&gt;getUploadRootDir().'/'.$this-&gt;getNameEn().'.jpg';
      }
  
    /**
     * @ORM\PrePersist()
     * @ORM\PreUpdate()
     */
      public function preUpload()
      {
        if (null !== $this-&gt;file)
        {
           $this-&gt;file = $this-&gt;getId() .'.'. $this-&gt;file-&gt;guessExtension();
        }
      }
  
      /**
       * @ORM\PostPersist()
       * @ORM\PostUpdate()
       */
      public function upload()
      {
        if (null === $this-&gt;file)
        {
          return;
        }
  
        $this-&gt;file-&gt;move($this-&gt;getUploadRootDir(), $this-&gt;file);
        unset($this-&gt;file);
      }
  
      /**
       * @ORM\PostRemove()
       */
       public function removeUpload()
       {
         if ($file = $this-&gt;getAbsolutePath())
         {
           unlink($file);
         }
       }
  </code></pre>
  
  <p>And the controller:</p>
  
  <pre><code>public function new_productAction(Request $request)
  {
    $product = new Items();
    $product-&gt;setPrice(0);
  
    $form = $this-&gt;createFormBuilder($product)
      -&gt;add('Type', 'choice', array(
        'choices'   =&gt; array('1' =&gt; 'Product', '0' =&gt; 'Article'),
        'required'  =&gt; false,))
      -&gt;add('Price',  'number')
      -&gt;add('nameEn', 'text')
      -&gt;add('file', 'file', array('label' =&gt; 'Image', 'required' =&gt; true))
      -&gt;getForm();
  
    if ($request-&gt;getMethod() == 'POST')
    {
      if ($form-&gt;isValid())
      {
        $form-&gt;bindRequest($request);
        $em = $this-&gt;getDoctrine()-&gt;getEntityManager();
        $em-&gt;persist($product);
        $em-&gt;flush();
  
        return new Response('&lt;html&gt;&lt;body&gt;Success!&lt;/body&gt;&lt;/html&gt;');
      }
    }
  
    return $this-&gt;render('MyBundle:Default:admin_page.html.twig', array(
      'form' =&gt; $form-&gt;createView(),
    ));
  }
  </code></pre>
  
  <p>Symfony version: 2.1.0</p>