Php Symfony 3-分别保存文件并多次上载

Php Symfony 3-分别保存文件并多次上载,php,file-upload,symfony-3.1,multi-upload,Php,File Upload,Symfony 3.1,Multi Upload,我正在学习Symfony并写博客 我设置了一个多文件上传到我的应用程序,文件被上传并保存。我的问题是,它们被保存在我的数据库中,像[“photo1.jpg”,“photo2.jpg”]这样的数组中 所以很不幸,我不能为每一张照片都有一个id 这是我的实体: <?php namespace Boreales\PlatformBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\HttpFoundatio

我正在学习Symfony并写博客

我设置了一个多文件上传到我的应用程序,文件被上传并保存。我的问题是,它们被保存在我的数据库中,像[“photo1.jpg”,“photo2.jpg”]这样的数组中

所以很不幸,我不能为每一张照片都有一个id

这是我的实体:

<?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->id;
    }

    /**
     * Set image
     *
     * @param string $image
     *
     * @return Blog
     */
    public function setImage($image)
    {
        $this->image = $image;

        return $this;
    }

    /**
     * Get image
     *
     * @return string
     */
    public function getImage()
    {
        return $this->image;
    }

    /**
     * Set categorie
     *
     * @param string $categorie
     *
     * @return Blog
     */
    public function setCategorie($categorie)
    {
        $this->categorie = $categorie;

        return $this;
    }

    /**
     * Get categorie
     *
     * @return string
     */
    public function getCategorie()
    {
        return $this->categorie;
    }

    /**
     * Set photographe
     *
     * @param string $photographe
     *
     * @return Blog
     */
    public function setPhotographe($photographe)
    {
        $this->photographe = $photographe;

        return $this;
    }

    /**
     * Get photographe
     *
     * @return string
     */
    public function getPhotographe()
    {
        return $this->photographe;
    }

    /**
     * Set date
     *
     * @param \DateTime $date
     *
     * @return Blog
     */
    public function setDate($date)
    {
        $this->date = $date;

        return $this;
    }

    /**
     * Get date
     *
     * @return \DateTime
     */
    public function getDate()
    {
        return $this->date;
    }

    public function __construct(){
      $this->date = new \Datetime();
    }

    public function getFile()
    {
      return $this->file;
    }

    public function setFile($file)
    {
      $this->file = $file;
      $this->image = array();
    }

    public function upload(){
      if(empty($this->file)){
        return;
      }
      foreach($this->file as $file)
      {
          $name = $file->getClientOriginalName();
          $path = $this->getUploadDir().$name;
          $this->image = $path;
          $file->move($this->getUploadRootDir(), $path);
          unset($file);
      }
    }

    public function getUploadDir(){
      return 'uploads/img/';
    }

    public function getUploadRootDir(){
      return __DIR__.'/../../../../web/'.$this->getUploadDir();
    }
}
我试着这样做foreach(),并将Blog实体中的$image更改为一个字符串,以便在每个文件中持久化,但也不起作用

谢谢你的帮助

<?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
            ->add('file', FileType::class, array(
              'multiple' => true,
              'data_class' => null
            ))
            ->add('categorie', EntityType::class, array(
              'class' => 'BorealesPlatformBundle:Categorie',
              'choice_label' => 'categorie'
            ))
            ->add('photographe', EntityType::class, array(
              'class' => 'BorealesPlatformBundle:Photographe',
              'choice_label' => 'photographe'
            ))
            ->add('enregistrer', SubmitType::class)
        ;
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Boreales\PlatformBundle\Entity\Blog'
        ));
    }
}
    public function addAction(Request $request)
{
  //Création de l'entité
  $photo = new Blog();
  $form = $this->get('form.factory')->create(BlogType::class, $photo);

  if($request->isMethod('POST') && $form->handleRequest($request)->isValid()){
    foreach($photo->getFile() as $image){
      $image->upload();
      /*
      $photo->upload();
      $em = $this->getDoctrine()->getManager();
      $em->persist($photo);
      $em->flush();*/
    }
    /*

    $request->getSession()->getFlashBag()->add('notice', 'Photo enregistrée.');
    return $this->redirectToRoute('galerie');*/
    return new Response('Ok');
  }
  return $this->render('BorealesPlatformBundle:Blog:add.html.twig', array(
    'form' => $form->createView()
  ));
}