title: Can't upload image in Symfony
body: <p>I want to upload an image in Symfony but the path is <code>null</code> in the database. I have the rest of champ in entity not null and the value write in Form.
  The path is null but the rest (name and updated_at) is not null and have value write in form.</p>
  
  <p>This is my Media.php:</p>
  
  <pre><code>&lt;?php
  
      namespace ventenligneBundle\Entity;
  
      use Doctrine\ORM\Mapping as ORM;
      use Symfony\Component\validator\Constraints as Assert;
  
      /**
       * Media
       *
       * @ORM\Table()
       * @ORM\Entity(repositoryClass="ventenligneBundle\Entity\MediaRepository")
       * @ORM\HasLifecycleCallbacks
       */
      class Media
      {
          /**
           * @var integer
           *
           * @ORM\Column(name="id", type="integer")
           * @ORM\Id
           * @ORM\GeneratedValue(strategy="AUTO")
           */
          private $id;
  
           /**
           * @var \DateTime
           *
           * @ORM\COlumn(name="updated_at",type="datetime", nullable=true)
           */
          private $updateAt;
  
          /**
           * @ORM\PostLoad()
           */
          public function postLoad()
          {
              $this-&gt;updateAt = new \DateTime();
          }
  
          /**
           * @ORM\Column(type="string",length=255)
           */
          public $name;
  
          /**
           * @ORM\Column(type="string",length=255, nullable=true)
           */
          public $path;
  
          public $file;
  
            public function getUploadRootDir()
          {
              return __dir__.'/../../../web/uploads/produit';
          }
  
          public function getAbsolutePath()
          {
              return null === $this-&gt;path ? null : $this-&gt;getUploadRootDir().'/'.$this-&gt;path;
          }
  
          public function getAssetPath()
          {
              return 'uploads/'.$this-&gt;path;
          }
  
          /*
           *@ORM\PrepPersist()
           * @ORM\PreUpdate()
           */
          public function preUpload()
          {
              $this-&gt;tempFile = $this-&gt;getAbsolutePath();
              $this-&gt;oldFile = $this-&gt;getPath();
              $this-&gt;updateAt = new \DateTime();
  
              if (null !== $this-&gt;file)
                  $this-&gt;path = sha1(uniqid(mt_rand(),true)).'.'.$this-&gt;file-&gt;guessExtension();
          }
  
          /**
           * @ORM\PostPersist()
           * @ORM\PostUpdate()
           */
          public function upload()
          {
                      $this-&gt;oldFile = $this-&gt;getPath();
  
              if (null !== $this-&gt;file) {
                  $this-&gt;file-&gt;move($this-&gt;getUploadRootDir(),$this-&gt;path);
                  //unset($this-&gt;file);
                      $this-&gt;path=$this-&gt;getAbsolutePath();
  
  
                  if ($this-&gt;oldFile != null) unlink($this-&gt;tempFile);
              }
          }
  
          /**
           * @ORM\PreRemove()
           */
          public function preRemoveUpload()
          {
              $this-&gt;tempFile = $this-&gt;getAbsolutePath();
          }
  
          /**
           * @ORM\PostRemove()
           */
          public function removeUpload()
          {
              if (file_exists($this-&gt;tempFile)) unlink($this-&gt;tempFile);
          }
  
  
          /**
           * Get id
           *
           * @return integer
           */
          public function getId()
          {
              return $this-&gt;id;
          }
  
  
  
          public function setPath($path)
          {
              $this-&gt;path = $path;
  
              return $this;
          }
  
          /**
           * Get path
           *
           * @return string
           */
          public function getPath()
          {
              return $this-&gt;path;
          }
  
  
          /**
           * Get name
           *
           * @return \string
           */
  
           public function getName()
          {
              return $this-&gt;name;
          }
  
  
  
          /**
           * Set updateAt
           *
           * @param \DateTime $updateAt
           * @return Media
           */
          public function setUpdateAt($updateAt)
          {
              $this-&gt;updateAt = $updateAt;
  
              return $this;
          }
  
          /**
           * Get updateAt
           *
           * @return \DateTime
           */
          public function getUpdateAt()
          {
              return $this-&gt;updateAt;
          }
  
          /**
           * Set name
           *
           * @param string $name
           * @return Media
           */
          public function setName($name)
          {
              $this-&gt;name = $name;
  
              return $this;
          }
  
  
      }
  </code></pre>
  
  <p>Produit.php:</p>
  
  <pre><code>&lt;?php
  
      namespace ventenligneBundle\Entity;
  
      use Doctrine\ORM\Mapping as ORM;
      use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  
      /**
       * Produit
       *
       * @ORM\Table()
       * @ORM\Entity(repositoryClass="ventenligneBundle\Entity\ProduitRepository")
       * @UniqueEntity("nom", message=" Une Produit existe déjà avec ce nom.")  */
      class Produit
      {
          /**
           * @var integer
           *
           * @ORM\Column(name="id", type="integer")
           * @ORM\Id
           * @ORM\GeneratedValue(strategy="AUTO")
           */
          private $id;
  
          /**
           * @var string
           *
           * @ORM\Column(name="nom", type="string", length=125)
           */
  
          private $nom;
  
          /**
           * @var string
           *
           * @ORM\Column(name="description", type="text")
           */
          private $description;
  
          /**
           * @var float
           *
           * @ORM\Column(name="prix", type="float")
           */
          private $prix;
  
          /**
           * @var boolean
           *
           * @ORM\Column(name="disponible", type="boolean")
           */
          private $disponible;
  
  
          /**
           * @var integer
           *
           * @ORM\Column(name="quantite", type="integer")
           */
          private $quantite;
  
          /**
          * @ORM\OneToOne(targetEntity="ventenligneBundle\Entity\Media", cascade={"persist"})
          */
            private $image;
  
  
          /**
           * @ORM\ManyToOne(targetEntity="Categorie",inversedBy="produit")
           * @ORM\JoinColumn(name="id_categorie", referencedColumnName="id")
           */
          private $categorie;
  
          /**
           * Get id
           *
           * @return integer 
           */
          public function getId()
          {
          return $this-&gt;id;
          }
  
          /**
           * Set nom
           *
           * @param string $nom
           * @return Produit
           */
          public function setNom($nom)
          {
          $this-&gt;nom = $nom;
  
          return $this;
          }
  
          /**
           * Get nom
           *
           * @return string 
           */
          public function getNom()
          {
          return $this-&gt;nom;
          }
  
          /**
           * Set description
           *
           * @param string $description
           * @return Produit
           */
          public function setDescription($description)
          {
          $this-&gt;description = $description;
  
          return $this;
          }
  
          /**
           * Get description
           *
           * @return string 
           */
          public function getDescription()
          {
          return $this-&gt;description;
          }
  
          /**
           * Set prix
           *
           * @param float $prix
           * @return Produit
           */
          public function setPrix($prix)
          {
          $this-&gt;prix = $prix;
  
          return $this;
          }
  
          /**
           * Get prix
           *
           * @return float 
           */
          public function getPrix()
          {
          return $this-&gt;prix;
          }
  
          /**
           * Set disponible
           *
           * @param boolean $disponible
           * @return Produit
           */
          public function setDisponible($disponible)
          {
          $this-&gt;disponible = $disponible;
  
          return $this;
          }
  
          /**
           * Get disponible
           *
           * @return boolean 
           */
          public function getDisponible()
          {
          return $this-&gt;disponible;
          }
  
          /**
           * Set image
           *
           * @param \ventenligneBundle\Entity\Media $image
           * @return Produit
           */
          public function setImage(\ventenligneBundle\Entity\Media $image = null)
          {
          $this-&gt;image = $image;
  
          return $this;
          }
  
          /**
           * Get image
           *
           * @return \ventenligneBundle\Entity\Media 
           */
          public function getImage()
          {
          return $this-&gt;image;
          }
  
          /**
           * Set tva
           *
           * @param \ventenligneBundle\Entity\Tva $tva
           * @return Produit
           */
          public function setTva(\ventenligneBundle\Entity\Tva $tva = null)
          {
          $this-&gt;tva = $tva;
  
          return $this;
          }
  
          /**
           * Get tva
           *
           * @return \ventenligneBundle\Entity\Tva 
           */
          public function getTva()
          {
          return $this-&gt;tva;
          }
  
          /**
           * Set categorie
           *
           * @param \ventenligneBundle\Entity\Categorie $categorie
           * @return Produit
           */
          public function setCategorie(\ventenligneBundle\Entity\Categorie $categorie = null)
          {
          $this-&gt;categorie = $categorie;
  
          return $this;
          }
  
          /**
           * Get categorie
           *
           * @return \ventenligneBundle\Entity\Categorie 
           */
          public function getCategorie()
          {
          return $this-&gt;categorie;
          }
  
          /**
           * Set quantite
           *
           * @param integer $quantite
           * @return Produit
           */
          public function setQuantite($quantite)
          {
          $this-&gt;quantite = $quantite;
  
          return $this;
          }
  
          /**
           * Get quantite
           *
           * @return integer 
           */
          public function getQuantite()
          {
          return $this-&gt;quantite;
          }
      }
  </code></pre>
  
  <p>ProduitType.php:</p>
  
  <pre><code>&lt;?php
  
      namespace ventenligneBundle\Form;
  
      use Symfony\Component\Form\AbstractType;
      use Symfony\Component\Form\FormBuilderInterface;
      use Symfony\Component\OptionsResolver\OptionsResolverInterface;
      use ventenligneBundle\Form\MediaType;
  
      class ProduitType extends AbstractType
      {
          /**
           * @param FormBuilderInterface $builder
           * @param array $options
           */
          public function buildForm(FormBuilderInterface $builder, array $options)
          {
              $builder
                  -&gt;add('nom')
                  -&gt;add('description')
                  -&gt;add('prix')
                  -&gt;add('disponible')
                  -&gt;add('quantite')
                  -&gt;add('image' new MediaType())
                  -&gt;add('categorie')
  
              ;
          }
  
          /**
           * @param OptionsResolverInterface $resolver
           */
          public function setDefaultOptions(OptionsResolverInterface $resolver)
          {
              $resolver-&gt;setDefaults(array(
                  'data_class' =&gt; 'ventenligneBundle\Entity\Produit'
              ));
          }
  
          /**
           * @return string
           */
          public function getName()
          {
              return 'ventenlignebundle_produit';
          }
      }
  </code></pre>
  
  <p>This is a screenshot of the database:</p>
  
  <p><a href="https://i.stack.imgur.com/vBpgU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vBpgU.png" alt="enter image description here"></a></p>