title: SonataAdminBundle file upload: Error
body: <p>I'm a student actually working on my own Symfony2 project and it's few days now I can't find a solution to my problem.</p>
  
  <p>UPDATE: 03.09.2013</p>
  
  <p>I have the current version of symfony and of the sonata admin bundle and need a form in my admin with multiple image uploads.</p>
  
  <p>The following code I present is based on this installation documentation:</p>
  
  <p><a href="http://sonata-project.org/bundles/admin/master/doc/reference/recipe_file_uploads.html" rel="nofollow">http://sonata-project.org/bundles/admin/master/doc/reference/recipe_file_uploads.html</a></p>
  
  <p>In my case I have an entity Projects (Pf\Bundle\BlogBundle\Entity\Projects.php) in my bundle. In this entity I have $image1 (which is the equivalent of filename in the doc) and of course the unmapped property file. All strings and configured as it has to be. (Note that I use image1 instead of filename in my case // documentation).</p>
  
  <pre><code>&lt;?php
  
  namespace Pf\Bundle\BlogBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Symfony\Component\HttpFoundation\File\UploadedFile;
  use Symfony\Component\Validator\Constraints as Assert;
  
  
  /**
   * Projects
   *
   * @ORM\Table()
   * @ORM\Entity(repositoryClass="Pf\Bundle\BlogBundle\Entity\ProjectRepository")
    * @ORM\HasLifecycleCallbacks()
   */
  class Projects
  {
  
      const SERVER_PATH_TO_IMAGE_FOLDER = '/uploads/medias';
  
      /**
       * Unmapped property to handle file uploads
       */
      private $file;
  
      /**
       * Sets file.
       *
       * @param UploadedFile $file
       */
      public function setFile(UploadedFile $file = null)
      {
          $this-&gt;file = $file;
      }
  
      /**
       * Get file.
       *
       * @return UploadedFile
       */
      public function getFile()
      {
          return $this-&gt;file;
      }
  
      /**
       * Manages the copying of the file to the relevant place on the server
       */
      public function upload()
      {
          // the file property can be empty if the field is not required
          if (null === $this-&gt;getFile()) {
              return;
          }
          // we use the original file name here but you should
          // sanitize it at least to avoid any security issues
  
          // move takes the target directory and target filename as params
          $this-&gt;getFile()-&gt;move(
              Projects::SERVER_PATH_TO_IMAGE_FOLDER,
              $this-&gt;getFile()-&gt;getClientOriginalName()
          );
  
          // set the path property to the filename where you've saved the file
          $this-&gt;image1 = $this-&gt;getFile()-&gt;getClientOriginalName();
  
          // clean up the file property as you won't need it anymore
          $this-&gt;setFile(null);
      }
  
      /**
       * Lifecycle callback to upload the file to the server
       */
      public function lifecycleFileUpload() {
          $this-&gt;upload();
      }
  
      /**
       * Updates the hash value to force the preUpdate and postUpdate events to fire
       */
      public function refreshUpdated() {
          $this-&gt;setUpdated(date('Y-m-d H:i:s'));
      }
  
      /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @var string
       *
       * @ORM\Column(name="image1", type="string", length=100)
       */
      private $image1;
  
      //...
  
      /**
       * @var datetime
       *
       * @ORM\Column(name="updated", nullable=true)
       */
      private $updated;
  
       /**
       * Set updated
       *
       * @param string $updated
       * @return Projects
       */
      public function setUpdated($updated)
      {
          $this-&gt;updated = $updated;
  
          return $this;
      }
  
      /**
       * Get updated
       *
       * @return string 
       */
      public function getUpdated()
      {
          return $this-&gt;updated;
      }
  }
  </code></pre>
  
  <p>I also have an admin controller (Pf\Bundle\BlogBundle\Admin\ProjectsAdmin.php) where I have the following form (created in the "sonata admin way"):</p>
  
  <pre><code>&lt;?php
  namespace Pf\Bundle\BlogBundle\Admin;
  
  use Sonata\AdminBundle\Admin\Admin;
  use Sonata\AdminBundle\Datagrid\ListMapper;
  use Sonata\AdminBundle\Datagrid\DatagridMapper;
  use Sonata\AdminBundle\Validator\ErrorElement;
  
  use Sonata\AdminBundle\Form\FormMapper;
  
  class ProjectsAdmin extends Admin
  {
      // setup the default sort column and order
      protected $datagridValues = array(
          '_sort_order' =&gt; 'DESC',
          '_sort_by' =&gt; 'id'
      );
  
      protected function configureFormFields(FormMapper $formMapper)
      {
          $formMapper
              -&gt;add('file', 'file', array('required' =&gt; false, 'data_class' =&gt; null))
              -&gt;add('image2',  'text')
              -&gt;add('image3', 'text')
              -&gt;add('link', 'text')
              -&gt;add('download_link', 'text')
              -&gt;add('content1', 'text')
              -&gt;add('content2', 'text')
              -&gt;add('title', 'text')
              -&gt;add('thumbnail', 'text')
          ;
      }
  
      public function prePersist($projects) {
          $this-&gt;manageFileUpload($projects);
      }
  
      public function preUpdate($projects) {
          $this-&gt;manageFileUpload($projects);
      }
  
      private function manageFileUpload($projects) {
          if ($projects-&gt;getFile()) {
              $projects-&gt;refreshUpdated();
          }
      }
  
      protected function configureDatagridFilters(DatagridMapper $datagridMapper)
      {
          $datagridMapper
              -&gt;add('title')
          ;
      }
  
      protected function configureListFields(ListMapper $listMapper)
      {
          $listMapper
              -&gt;addIdentifier('title')
          ;
      }
  }
  </code></pre>
  
  <p>I have few problems:</p>
  
  <ul>
  <li><p>If I try to CREATE A NEW project, the image1 appears to be null
  every time I try to upload. I can make it nullable in the entity but
  then I don't get any url at all in the database</p>
  
  <p>An exception occurred while executing 'INSERT INTO Projects...
  Integrity constraint violation: 1048 Column 'image1' cannot be null</p></li>
  <li><p>By editing an existing project in the admin, it seems to work..well nearly..I don't get any errors when uploading the file BUT I get a temporary path in the database and no file has been moved in the good folder.</p></li>
  </ul>
  
  <p>It looks like the upload function isn't called. I try to debug it but can't find a solution.</p>
  
  <p>I have followed step by step the documentation. The only difference is that I don't use any .yaml file to configure my entity.. Does I have to? I'm using annotations on my symfony, I guess it's not good to use orm.yaml and annotation in the same time...right?</p>
  
  <p>Any help is more than welcome!</p>