title: Symfony2 - Undefined property in entity for file upload
body: <p>I am getting the following error when trying to upload a file, it's odd because I've used the same code on other projects without any problems/errors.</p>
  
  <p>What am I missing here?</p>
  
  <pre><code>Notice: Undefined property: Acme\DemoBundle\Entity\Article::$file in /var/www/html/InsideFight/src/Acme/DempBundle/Entity/Article.php line 277
  </code></pre>
  
  <p><strong>The problem line is:</strong> </p>
  
  <pre><code>if (null !== $this-&gt;file) {
  </code></pre>
  
  <p>I do not have any file upload code in my controller it's being handled in the entity.</p>
  
  <p><strong>Entity</strong></p>
  
  <pre><code>public $file;
  
  
  public function getUploadDir()
  {
      return 'images/';
  }
  
  public function getUploadRootDir()
  {
      return __DIR__ . '/../../../../web/' . $this-&gt;getUploadDir();
  }
  
  public function getWebPath()
  {
      return null === $this-&gt;image ? null : $this-&gt;getUploadDir() . '/' . $this-&gt;image;
  }
  
  public function getAbsolutePath()
  {
      return null === $this-&gt;image ? null : $this-&gt;getUploadRootDir() . '/' . $this-&gt;image;
  }
  
  /**
   * @ORM\PrePersist()
   * @ORM\PreUpdate()
   */
  public function preUpload()
  {
      if (null !== $this-&gt;file) {
          $this-&gt;image = uniqid() . '.' . $this-&gt;file-&gt;guessExtension();
      }
  }
  
  /**
   * @ORM\PostPersist()
   * @ORM\PostUpdate()
   */
  public function upload()
  {
      if (null === $this-&gt;file) {
          return;
      }
  
      // If there is an error when moving the file, an exception will
      // be automatically thrown by move(). This will properly prevent
      // the entity from being persisted to the database on error
      $this-&gt;file-&gt;move($this-&gt;getUploadRootDir(), $this-&gt;image);
  
      unset($this-&gt;file);
  }
  /**
   * @ORM\PostRemove()
   */
  public function removeUpload()
  {
      if ($file = $this-&gt;getAbsolutePath()) {
          unlink($file);
      }
  }
  </code></pre>