title: Symfony 3 - Persist files separately multiple upload
body: <p>I'm learning Symfony and making a blog.</p>
  
  <p>I set up a multiple file upload into my app and the files are uploaded and persisted. My problem is that they are saved in my database into an array like ["photo1.jpg", "photo2.jpg"]. </p>
  
  <p>So unfortunately I can't have an id for each picture. </p>
  
  <p>Here is my Entity : </p>
  
  <pre><code>&lt;?php
  
  namespace Boreales\PlatformBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Symfony\Component\HttpFoundation\File\UploadedFile;
  use Symfony\Component\Validator\Constraints as Assert;
  /**
   * Blog
   *
   * @ORM\Table(name="blog")
   * @ORM\Entity(repositoryClass="Boreales\PlatformBundle\Repository\BlogRepository")
   * @ORM\HasLifecycleCallbacks
   */
  class Blog
  {
      /**
       * @var int
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       *
       * @ORM\Column(name="image", type="simple_array")
       *
       */
      private $image;
      /**
      * @Assert\NotBlank(message="Une image doit être jointe.")
      */
      private $file;
  
      /**
       * @var int
       *
       * @ORM\ManyToOne(targetEntity="Boreales\PlatformBundle\Entity\Categorie")
       * @ORM\JoinColumn(name="categorie_id",referencedColumnName="id")
       * @Assert\NotBlank(message="Le nom d'un photographe doit être renseigné.")
       */
      private $categorie;
  
      /**
       * @var int
       *
       * @ORM\ManyToOne(targetEntity="Boreales\PlatformBundle\Entity\Photographe")
       * @ORM\JoinColumn(name="photographe_id",referencedColumnName="id")
       * @Assert\NotBlank(message="Le nom d'un photographe doit être renseigné.")
       */
      private $photographe;
  
      /**
       * @var \DateTime
       *
       * @ORM\Column(name="date", type="datetime")
       */
      private $date;
  
      /**
       * Get id
       *
       * @return int
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set image
       *
       * @param string $image
       *
       * @return Blog
       */
      public function setImage($image)
      {
          $this-&gt;image = $image;
  
          return $this;
      }
  
      /**
       * Get image
       *
       * @return string
       */
      public function getImage()
      {
          return $this-&gt;image;
      }
  
      /**
       * Set categorie
       *
       * @param string $categorie
       *
       * @return Blog
       */
      public function setCategorie($categorie)
      {
          $this-&gt;categorie = $categorie;
  
          return $this;
      }
  
      /**
       * Get categorie
       *
       * @return string
       */
      public function getCategorie()
      {
          return $this-&gt;categorie;
      }
  
      /**
       * Set photographe
       *
       * @param string $photographe
       *
       * @return Blog
       */
      public function setPhotographe($photographe)
      {
          $this-&gt;photographe = $photographe;
  
          return $this;
      }
  
      /**
       * Get photographe
       *
       * @return string
       */
      public function getPhotographe()
      {
          return $this-&gt;photographe;
      }
  
      /**
       * Set date
       *
       * @param \DateTime $date
       *
       * @return Blog
       */
      public function setDate($date)
      {
          $this-&gt;date = $date;
  
          return $this;
      }
  
      /**
       * Get date
       *
       * @return \DateTime
       */
      public function getDate()
      {
          return $this-&gt;date;
      }
  
      public function __construct(){
        $this-&gt;date = new \Datetime();
      }
  
      public function getFile()
      {
        return $this-&gt;file;
      }
  
      public function setFile($file)
      {
        $this-&gt;file = $file;
        $this-&gt;image = array();
      }
  
      public function upload(){
        if(empty($this-&gt;file)){
          return;
        }
        foreach($this-&gt;file as $file)
        {
            $name = $file-&gt;getClientOriginalName();
            $path = $this-&gt;getUploadDir().$name;
            $this-&gt;image = $path;
            $file-&gt;move($this-&gt;getUploadRootDir(), $path);
            unset($file);
        }
      }
  
      public function getUploadDir(){
        return 'uploads/img/';
      }
  
      public function getUploadRootDir(){
        return __DIR__.'/../../../../web/'.$this-&gt;getUploadDir();
      }
  }
  </code></pre>
  
  <p>My Form :</p>
  
  <pre><code>&lt;?php
  
  namespace Boreales\PlatformBundle\Form;
  
  use Boreales\PlatformBundle\Repository\BlogRepository;
  use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\Extension\Core\Type\FileType;
  use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  use Symfony\Component\Form\Extension\Core\Type\TextType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\Form\FormEvent;
  use Symfony\Component\Form\FormEvents;
  use Symfony\Component\OptionsResolver\OptionsResolver;
  
  class BlogType extends AbstractType
  {
      /**
       * @param FormBuilderInterface $builder
       * @param array $options
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('file', FileType::class, array(
                'multiple' =&gt; true,
                'data_class' =&gt; null
              ))
              -&gt;add('categorie', EntityType::class, array(
                'class' =&gt; 'BorealesPlatformBundle:Categorie',
                'choice_label' =&gt; 'categorie'
              ))
              -&gt;add('photographe', EntityType::class, array(
                'class' =&gt; 'BorealesPlatformBundle:Photographe',
                'choice_label' =&gt; 'photographe'
              ))
              -&gt;add('enregistrer', SubmitType::class)
          ;
      }
  
      /**
       * @param OptionsResolver $resolver
       */
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'Boreales\PlatformBundle\Entity\Blog'
          ));
      }
  }
  </code></pre>
  
  <p>And my action in the Controller : </p>
  
  <pre><code>    public function addAction(Request $request)
  {
    //Création de l'entité
    $photo = new Blog();
    $form = $this-&gt;get('form.factory')-&gt;create(BlogType::class, $photo);
  
    if($request-&gt;isMethod('POST') &amp;&amp; $form-&gt;handleRequest($request)-&gt;isValid()){
      foreach($photo-&gt;getFile() as $image){
        $image-&gt;upload();
        /*
        $photo-&gt;upload();
        $em = $this-&gt;getDoctrine()-&gt;getManager();
        $em-&gt;persist($photo);
        $em-&gt;flush();*/
      }
      /*
  
      $request-&gt;getSession()-&gt;getFlashBag()-&gt;add('notice', 'Photo enregistrée.');
      return $this-&gt;redirectToRoute('galerie');*/
      return new Response('Ok');
    }
    return $this-&gt;render('BorealesPlatformBundle:Blog:add.html.twig', array(
      'form' =&gt; $form-&gt;createView()
    ));
  }
  </code></pre>
  
  <p>I tried to do this foreach() and change my $image in the Blog entity into a string to persist at each file but it doesn't work either.</p>
  
  <p>Thanks for helping !</p>