title: symfony vich uploader image_name cannot be null
body: <p>I tried to follow documentation of VichUploaderBundle but things doesn't seem to work as expected.
  I get the error </p>
  
  <blockquote>
    <p>SQLSTATE[23000]: Integrity constraint violation: 1048 Column
    'image_name' cannot be null</p>
  </blockquote>
  
  <p>Of course questions about it were asked many times but I didn't find any suitable answer to them.</p>
  
  <p>So my config.yml looks like this:</p>
  
  <pre><code>vich_uploader:
      db_driver: orm
      mappings:
          image:
              uri_prefix:         /images
              upload_destination: '%kernel.project_dir%/web/images'
              inject_on_load:     true
              namer:              vich_uploader.namer_uniqid
  </code></pre>
  
  <p>I've got Producer Entity:</p>
  
  <pre><code>/**
   * Products table
   *
   * @author gallareton
   *
   * @ORM\Table(name="producer")
   * @ORM\Entity(repositoryClass="ShopBundle\Repository\ProducerRepository")
   */
  class Producer
  {
      /**
       * Constructor
       */
      public function __construct() {
          $this-&gt;products = new ArrayCollection();
      }
  
      /**
       * @ORM\Column(type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @ORM\Column(type="string", length=25, unique=true)
       */
      private $name;
  
      /**
       * One Producer has Many Products
       * @ORM\OneToMany(targetEntity="Product", mappedBy="producer")
       */
      private $products;
  
      /**
       * One Producer has One Image
       * @ORM\OneToOne(targetEntity="\AppBundle\Entity\Image", cascade={"persist"})
       */
      private $image;
  </code></pre>
  
  <p>Image Entity (almost copied from documentation):</p>
  
  <pre><code>/**
   * @ORM\Entity
   * @Vich\Uploadable
   * @ORM\HasLifecycleCallbacks
   */
  class Image
  {
      /**
       * @ORM\Id
       * @ORM\Column(type="integer")
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * NOTE: This is not a mapped field of entity metadata, just a simple property.
       *
       * @Vich\UploadableField(mapping="image", fileNameProperty="imageName", size="imageSize")
       *
       * @var File
       */
      private $imageFile;
  
      /**
       * @ORM\Column(type="string", length=255)
       *
       * @var string
       */
      private $imageName;
  
      /**
       * @ORM\Column(type="integer")
       *
       * @var integer
       */
      private $imageSize;
  
      /**
       * @ORM\Column(type="datetime")
       *
       * @var \DateTime
       */
      private $updatedAt;
  
      /**
       * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
       * of 'UploadedFile' is injected into this setter to trigger the  update. If this
       * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
       * must be able to accept an instance of 'File' as the bundle will inject one here
       * during Doctrine hydration.
       *
       * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
       *
       * @return Image
       */
      public function setImageFile(File $image = null)
      {
          $this-&gt;imageFile = $image;
  
          if ($image) {
              // It is required that at least one field changes if you are using doctrine
              // otherwise the event listeners won't be called and the file is lost
              $this-&gt;updatedAt = new \DateTimeImmutable();
          }
  
          return $this;
      }
  
      /**
       * @return File|null
       */
      public function getImageFile()
      {
          return $this-&gt;imageFile;
      }
  
      /**
       * @param string $imageName
       *
       * @return Image
       */
      public function setImageName($imageName)
      {
          $this-&gt;imageName = $imageName;
  
          return $this;
      }
  
      /**
       * @return string|null
       */
      public function getImageName()
      {
          return $this-&gt;imageName;
      }
  
      /**
       * @param integer $imageSize
       *
       * @return Image
       */
      public function setImageSize($imageSize)
      {
          $this-&gt;imageSize = $imageSize;
  
          return $this;
      }
  
      /**
       * @return integer|null
       */
      public function getImageSize()
      {
          return $this-&gt;imageSize;
      }
  }
  </code></pre>
  
  <p>And a 2 Form classes, one embeded into another:
  ProducerImageType:</p>
  
  <pre><code>/**
   * Created by PhpStorm.
   * User: gallareton
   * Date: 16.11.2017
   * Time: 17:55
   */
  class ProducerImageType extends AbstractForm
  {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder-&gt;add('imageFile', VichImageType::class, [
              'required' =&gt; false,
              'allow_delete' =&gt; true,
              'label' =&gt; false,
          ]);
      }
  
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; Image::class,
          ));
      }
  }
  </code></pre>
  
  <p>ProducerForm:</p>
  
  <pre><code>/**
   * Created by PhpStorm.
   * User: gallareton
   * Date: 16.11.2017
   * Time: 17:55
   */
  class ProducerForm extends AbstractForm
  {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder-&gt;add(
              'id',
              HiddenType::class
          );
  
          $builder-&gt;add(
              'name',
              TextType::class,
              array(
                  'label' =&gt; 'app.name',
                  'constraints' =&gt; array(
                      new Length(
                          array(
                              'max' =&gt; 25,
                          )
                      ),
                  ),
              )
          );
  
          $builder-&gt;add(
              'image',
              ProducerImageType::class,
              array(
                  'label' =&gt; 'app.image',
              )
          );
  
          $builder-&gt;add(
              'save',
              SubmitType::class,
              array('label' =&gt; 'app.save')
          );
      }//end BuildForm()
  
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class'      =&gt; Producer::class,
              'crsf_protection' =&gt; true,
              'crsf_field_name' =&gt; '_token',
              'crsf_token_id'   =&gt; 'producer'
          ));
      }//end configureOptions()
  }
  </code></pre>
  
  <p>I tried to launch the sandbox, but I got so many errors that I gave up after 1 hour. Looked up files thou and it seems like I have done everything properly.. Probably I'm missing some minor detail. I thought about setting filename and filesize manually in <code>setImageFile</code> method but this would be only a workaround and I'm trying to avoid such solutions as everyone should.</p>
  
  <p>Action of submitting the form may be important too, so here is the action:
  notice: <code>$this-&gt;db()</code> is my shortcut for <code>$this-&gt;getDoctrine()-&gt;getManager()</code> and <code>$this-&gt;getRepo()</code> is shortcut for <code>$this-&gt;getDoctrine()-&gt;getManager()-&gt;getRepository()</code></p>
  
  <pre><code>/**
   * @Route("/edit/{id}", name="producerAdminEdit")
   * @param Request $request
   * @param int $id
   * @return \Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\Response
   */
  public function editAction(Request $request, $id = null)
  {
      $this-&gt;setMenuTab('cars', 'admin');
  
      if (!empty($id)) {
          $producer = $this-&gt;getRepo(Producer::class)-&gt;find($id);
          if (empty($producer)) {
              throw $this-&gt;createNotFoundException();
          }
  
          $this-&gt;setTitle('admin.cars.producers.edit');
      } else {
          $producer = new Producer();
          $this-&gt;setTitle('admin.cars.producers.new');
      }
  
      $form = $this-&gt;createForm(ProducerForm::class, $producer);
      $form-&gt;handleRequest($request);
  
      if ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()) {
          $producer = $form-&gt;getData();
          dump($producer);
  
          $this-&gt;db()-&gt;persist($producer);
          $this-&gt;db()-&gt;flush();
  
          return $this-&gt;redirectToRoute('carAdmin', array('tab' =&gt; 'producers'));
      }
  
      $this-&gt;setVariables(
          array(
              'form'     =&gt; $form-&gt;createView(),
              'producer' =&gt; $producer,
          )
      );
  
      return $this-&gt;response();
  
  }//end editAction()
  </code></pre>