title: VichUploaderBundle: adding Image through Post From not working
body: <p>I'm using VichUploader in my Symfony2.3 application. I created one to one relationship between my Post entity and Image entity and I'm using  Data Transformer for adding images through Post Form. Everything is working well, I'm getting no exception but post_id is not saved in Image table in database. Probably because of that every image I want to post is broken.</p>
  
  <p>I would appreciate any help!</p>
  
  <p>Image entity:</p>
  
  <pre><code>namespace BlogBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Doctrine\Common\Collections\ArrayCollection;
  use Symfony\Component\Validator\Constraints as Assert;
  use Symfony\Component\HttpFoundation\File\UploadedFile;
  use Symfony\Component\HttpFoundation\File\File;
  use Vich\UploaderBundle\Mapping\Annotation as Vich;
  
  /**
   * Class Image.
   *
   * @package Model
   * @author Monika Malinowska
   *
   * @ORM\Table(name="images")
   * @ORM\Entity(repositoryClass="BlogBundle\Repository\Image")
   * @Vich\Uploadable
   */
  
  class Image
  {
      /**
       * @ORM\Id
       * @ORM\Column(
       *     type="integer",
       *     nullable=false,
       *     options={
       *         "unsigned" = true
       *     }
       * )
       * @ORM\GeneratedValue(strategy="IDENTITY")
       * @var integer $id
       */
  private $id;
  
  /**
   * @ORM\OneToOne(targetEntity="Post", inversedBy="image")
   * @ORM\JoinColumn(name="post_id", referencedColumnName="id")
   */
  protected $post;
  
  /**
   *
   * @Vich\UploadableField(mapping="image", fileNameProperty="imageName")
   *
   * @var File
   */
  private $imageFile;
  
  /**
   * @ORM\Column(type="string", length=255)
   *
   * @var string
   */
  private $imageName;
  
  /**
   * @ORM\Column(type="datetime")
   *
   * @var \DateTime
   */
  private $updatedAt;
  
  /**
   *
   * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
   *
   * @return Image
   */
  
  public function __construct()
  {
      $this-&gt;updatedAt= new \DateTime();
  }
  
  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 \DateTime('now');
      }
  
      return $this;
  }
  
  /**
   * @return File
   */
  public function getImageFile()
  {
      return $this-&gt;imageFile;
  }
  
  /**
   * @param string $imageName
   *
   * @return Image
   */
  public function setImageName($imageName)
  {
      $this-&gt;imageName = $imageName;
  
      return $this;
  }
  
  /**
   * @return string
   */
  public function getImageName()
  {
      return $this-&gt;imageName;
  }
  
  /**
   * Set Id.
   *
   * @param integer $id Id
   */
  public function setId($id)
  {
      $this-&gt;id = $id;
  }
  
  /**
   * Get Id.
   *
   * @return integer Result
   */
  public function getId()
  {
      return $this-&gt;id;
  }
  
  /**
   * Set Post.
   *
   * @param integer $post Post
   */
  public function setPost($post)
  {
      $this-&gt;post = $post;
  }
  
  /**
   * Get Post.
   *
   * @return integer Result
   */
  public function getPost()
  {
      return $this-&gt;post;
  }}
  </code></pre>
  
  <p>Post Entity:</p>
  
  <pre><code>namespace BlogBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Doctrine\Common\Collections\ArrayCollection;
  use Symfony\Component\Validator\Constraints as Assert;
  use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  use Symfony\Component\HttpFoundation\File\UploadedFile;
  
  /**
   * Class Post.
   *
   * @package Model
   *
   * @ORM\Table(name="posts")
   * @ORM\Entity(repositoryClass="BlogBundle\Repository\Post")
   */
  class Post
  {
  /**
   * @ORM\Id
   * @ORM\Column(
   *     type="integer",
   *     nullable=false,
   *     options={
   *         "unsigned" = true
   *     }
   * )
   * @ORM\GeneratedValue(strategy="IDENTITY")
   * @var integer $id
   */
  private $id;
  
  ...
  
  /**
   * @ORM\OneToOne(targetEntity="Image", mappedBy="post")
   */
  protected $image;
  
  
  /**
   * Get Id.
   *
   * @return integer Result
   */
  public function getId()
  {
      return $this-&gt;id;
  }
  
  
  /**
   * Set Id.
   *
   * @param integer $id Id
   */
  public function setId($id)
  {
      $this-&gt;id = $id;
  }
  
  /**
   * Set image.
   *
   * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
   * @return $image
   */
  public function setImage($image)
  {
      $this-&gt;image = $image;
      return $this;
  }
  
  /**
   * Get image.
   *
   * @return file Image
   */
  public function getImage()
  {
      return $this-&gt;image;
  }
  
  /**
   * Add images.
   *
   * @param \BlogBundle\Entity\Image $image
   * @return Image
   */
  public function addImage(\BlogBundle\Entity\Image $image)
  {
      $this-&gt;image = $image;
      $image-&gt;setImage($this);
      return $this;
  }
  
  /**
   * Remove image
   *
   * @param \BlogBundle\Entity\Image $image
   */
  public function removeImage(\BlogBundle\Entity\Image $image)
  {
      $this-&gt;$image-&gt;removeElement($image);
      $image-&gt;setImage(null);
  }
  </code></pre>
  
  <p>ImageType</p>
  
  <pre><code>namespace BlogBundle\Form;
  
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  use Symfony\Component\Form\FormTypeExtensionInterface;
  use Symfony\Component\HttpFoundation\File\File;
  use Vich\UploaderBundle\Form\Type\VichImageType;
  
  /**
   * Class ImageType.
   *
   * @package BlogBundle\Form
   */
  class ImageType extends AbstractType
  {
  
      /**
       * Form builder.
       *
       * @param FormBuilderInterface $builder Form builder
       * @param array $options Form options
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
  
          $builder-&gt;add(
              'id',
              'hidden'
          );
          if (isset($options['validation_groups'])
              &amp;&amp; count($options['validation_groups'])
              &amp;&amp; !in_array('image-delete', $options['validation_groups'])
          ) {
             // $builder-&gt;add('name', 'file', array('label' =&gt; 'Obrazek (format JPG)'));
              //$builder-&gt;add('name', 'text');
              $builder-&gt;add('imageFile', 'vich_image');
          }
          $builder-&gt;add(
              'save',
              'submit',
              array(
                  'label' =&gt; 'Save'
              )
          );
      }
  
      /**
       * Sets default options for form.
       *
       * @param OptionsResolverInterface $resolver
       */
      public function setDefaultOptions(OptionsResolverInterface $resolver)
      {
          $resolver-&gt;setDefaults(
              array(
                  'data_class' =&gt; 'BlogBundle\Entity\Image',
                  'validation_groups' =&gt; 'image-default',
              )
          );
      }
  
      /**
       * Getter for form name.
       *
       * @return string Form name
       */
      public function getName()
      {
          return 'image_form';
      }
  }
  </code></pre>
  
  <p>PostType</p>
  
  <pre><code>namespace BlogBundle\Form;
  
  use BlogBundle\Form\DataTransformer\TagDataTransformer;
  use BlogBundle\Form\DataTransformer\ImageDataTransformer;
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  use Symfony\Component\HttpFoundation\File\File;
  use BlogBundle\Form\ImageType;
  
  /**
   * Class PostType.
   *
   * @package BlogBundle\Form
   */
  class PostType extends AbstractType
  {
      /**
       * Form builder.
       *
       * @param FormBuilderInterface $builder Form builder
       * @param array $options Form options
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $imageDataTransformer = new ImageDataTransformer($options['image_model']);
  
          $builder-&gt;add(
              'id',
              'hidden'
          );
          if (isset($options['validation_groups'])
              &amp;&amp; count($options['validation_groups'])
              &amp;&amp; !in_array('post-delete', $options['validation_groups'])
          ) {
  ...
              $builder-&gt;add(
                  $builder
                      -&gt;create('image', 'file')
                      -&gt;addModelTransformer($imageDataTransformer)
              );
          }
          $builder-&gt;add(
              'save',
              'submit',
              array(
                  'label' =&gt; 'form.save'
              )
          );
      }
  
      /**
       * Sets default options for form.
       *
       * @param OptionsResolverInterface $resolver
       */
      public function setDefaultOptions(OptionsResolverInterface $resolver)
      {
          $resolver-&gt;setDefaults(
              array(
                  'data_class' =&gt; 'BlogBundle\Entity\Post',
                  'validation_groups' =&gt; 'post-default',
              )
          );
  
          $resolver-&gt;setRequired(array('image_model'));
          $resolver-&gt;setAllowedTypes(
              array(
                  'image_model' =&gt; 'Doctrine\Common\Persistence\ObjectRepository'
              )
          );
      }
  
      /**
       * Getter for form name.
       *
       * @return string Form name
       */
      public function getName()
      {
          return 'post_form';
      }
  }
  </code></pre>
  
  <p>config.yml</p>
  
  <pre><code>vich_uploader:
      db_driver: orm
  
      mappings:
          image:
              uri_prefix:         /assetic/images
              upload_destination: '%kernel.root_dir%/../web/assetic/images'
              namer:              vich_uploader.namer_uniqid
  
              inject_on_load:     false
              delete_on_update:   true
              delete_on_remove:   true
  </code></pre>