1

我的问题是在上传文件之前检索元数据。

我的配置文件:

vich_uploader:
    db_driver: orm
    mappings:
        media:
            uri_prefix:         /uploads/
            upload_destination: '%kernel.root_dir%/../web/uploads'
            inject_on_load:     false
            delete_on_update:   true
            delete_on_remove:   true

我有一个实体 MEDIA :

<?php

    namespace AppBundle\Entity;

    use Doctrine\ORM\Mapping as ORM;
    use Symfony\Component\HttpFoundation\File\File;
    use Vich\UploaderBundle\Mapping\Annotation as Vich;

    **
     * @ORM\Entity
     * @Vich\Uploadable
     */
    class Media
    {
        /**
         * @ORM\Id
         * @ORM\Column(type="integer")
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;

        /**
         *
         * @Vich\UploadableField(mapping="media", fileNameProperty="fileName",originalName="originalFileName")
         *
         * @var File
         */
         private $file;

         /**
          * @ORM\Column(type="string", length=50)
          */
         private $fileName;

         /**
          * @ORM\Column(type="string", length=255, nullable=false)
          */
         private $originalFileName;

         /**
          * @ORM\Column(type="datetime")
          */
         private $updatedAt;

         /**
          * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $file
          *
          * @return Media
          */
         public function setFile(File $file = null)
         {
             $this->file = $file;

             if ($file) {
                 $this->updatedAt = new \DateTimeImmutable();
             }

             return $this;
         }

         /**
          * @return File|null
          */
         public function getFile()
         {
             return $this->file;
         }

         /**
          * @param string $fileName
          *
          * @return Media
          */
         public function setFileName($fileName)
         {
             $this->fileName = $fileName;

             return $this;
         }

         /**
          * @return string|null
          */
         public function getFileName()
         {
             return $this->fileName;
         }

         /**
          * Get id
          *
          * @return integer
          */
         public function getId()
         {
             return $this->id;
         }

         /**
          * Set updatedAt
          *
          * @param \DateTime $updatedAt
          *
          * @return Media
          */
         public function setUpdatedAt($updatedAt)
         {
             $this->updatedAt = $updatedAt;

             return $this;
         }

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

         /**
          * Set originalFileName
          *
          * @param string $originalFileName
          *
          * @return Media
          */
         public function setOriginalFileName($originalFileName)
         {
             $this->originalFileName = $originalFileName;

             return $this;
         }

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

这是我的控制器:

   /**
     * Creates a new media entity.
     *
     * @Route("/new", name="media_new")
     * @Method({"GET", "POST"})
     *
     * @param Request $request
     *
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function newAction(Request $request)
    {
        $media = new Media();
        $form = $this->createForm(MediaType::class, $media);

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {

            $em = $this->getDoctrine()->getManager();

            $em->persist($media);
            $em->flush();
            return $this->redirectToRoute(
                'media_list'
            );
        }

        return $this->render(
            'media/new.html.twig',
            [
                'media' => $media,
                'form' => $form->createView(),
            ]
        );
    }

我的表格:

    <?php
    /**
     * Created by PhpStorm.
     * User: rafael
     * Date: 4/10/17
     * Time: 12:46 PM
     */

    namespace AppBundle\Form;

    use AppBundle\Entity\Media;
    use AppBundle\Entity\MediaDescriptionHelper;
    use Symfony\Bridge\Doctrine\Form\Type\EntityType;
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
    use Symfony\Component\Form\Extension\Core\Type\FileType;
    use Symfony\Component\Form\Extension\Core\Type\SubmitType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\OptionsResolver\OptionsResolver;

    class MediaType extends AbstractType
    {
        /**
         * @param FormBuilderInterface $builder
         * @param array $options
         */
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('file', FileType::class)
                ->add('save', SubmitType::class, [
                    'attr' => ['class' => 'btn-primary btn-block']
                ]);
        }

        /**
         * @param OptionsResolver $resolver
         */
        public function configureOptions(OptionsResolver $resolver)
        {
            $resolver->setDefaults(['data_class' => Media::class]);
        }
    }

问题在于这些值的映射:

@Vich\UploadableField(mapping="media", fileNameProperty="fileName",originalName="originalFileName")

当我提交表单时,这些值为 'null' :

使用参数 ["get_image_resultat_sans_cache2.php.jpeg", null, "2017-04-12 10:11:56" 执行 'INSERT INTO media (file_name, original_file_name, updated_at) VALUES (?, ?, ?)' 时发生异常]:

我对所有参数都有这些问题:

https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/usage.md

UploadableField 注解有几个选项。它们如下:

mapping:必需,在捆绑配置中指定要使用的映射名称; fileNameProperty:必需,将包含上传文件名称的属性; size:将包含上传文件的大小(以字节为单位)的属性; mimeType:将包含上传文件的 mime 类型的属性; originalName:将包含上传文件的原始名称的属性。

我看不出我做错了什么...

提交表单后,这是我的媒体(实体):

媒体 {#403 ▼ -id: null -file: UploadedFile {#15 ▼ -test: false -originalName: "get_image_resultat_sans_cache2.php.jpeg" -mimeType: "image/jpeg" -size: 203751 -error: 0 path: " /tmp" 文件名:"php9xsTdF" 基名:"php9xsTdF" 路径名:"/tmp/php9xsTdF" 扩展名:"" realPath:"/tmp/php9xsTdF" aTime: 2017-04-12 10:11:56 mTime: 2017-04 -12 10:11:56 cTime: 2017-04-12 10:11:56 inode: 6160452 size: 203751 perms: 0100600 owner: 1000 group: 1000 type: "file" writable: true 可读: true executable: false file: true dir: false link: false } -fileName: null -originalFileName: null

在上传文件之前设置元数据似乎是一个问题......

非常感谢提前...

4

1 回答 1

2

您使用哪个版本的 VichUploaderBundle?

注释的文档是指 dev-master 文档,而稳定版 (1.5.3) 不支持开箱即用的元数据注释。

可以看到 1.5.3 版本中的 Vich\UploaderBundle\Mapping\Annotation\UploadableField.php 只处理注解 'mapping' 和 'fileNameProperty'。

在 dev-master 中,它处理这些和大小、mimeType 和 originalName。

与 Vich\UploaderBundle\Metadata\Driver\AnnotationDriver 相同

如果你想在 1.5.3 版本中实现这一点,你需要创建一个 eventListener。

以下是 Vich 触发的事件:https ://github.com/dustin10/VichUploaderBundle/blob/1.5.3/Event/Events.php

于 2017-05-10T14:27:36.563 回答