1

我正在学习 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();
    }
}

我的表格:

<?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'
        ));
    }
}

我在 Controller 中的操作:

    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()
  ));
}

我尝试执行此操作 foreach() 并将博客实体中的 $image 更改为字符串以保留在每个文件中,但它也不起作用。

感谢您的帮助!

4

0 回答 0