代码之家  ›  专栏  ›  技术社区  ›  Krleza

Vich和gaufrette没有在sonata admin中保存文件

  •  1
  • Krleza  · 技术社区  · 8 年前

    我正在尝试使用Vich将上传链接到Sonata Admin中的一个实体。

    所有的配置都完成了,但文件没有上传,我找不到错误。

    问题是,当y尝试上载文件时,一切似乎都很好,Sonata将数据保存在所有数据库字段中,并将文件上载到sistem中的/tmp文件夹中,此外,Sonata在数据库的补丁字段中打印tmp路由。但该文件从未到达Gaufrete中设置的文件夹,也没有生成唯一的名称。

    admin类:

    <?php
    
    namespace DownloadFileAdminBundle\Admin;
    
    use Sonata\AdminBundle\Admin\AbstractAdmin;
    use Sonata\AdminBundle\Admin\Admin;
    use Sonata\AdminBundle\Datagrid\DatagridMapper;
    use Sonata\AdminBundle\Datagrid\ListMapper;
    use Sonata\AdminBundle\Form\FormMapper;
    
    class DownloadFileAdmin extends Admin
    {
        const FILE_MAX_SIZE = 2 * 1024 * 1024; // 2 megas
    
        /**
         * @param FormMapper $formMapper
         */
        protected function configureFormFields(FormMapper $formMapper)
        {
            $fileOptions = array(
                'label' => 'Archivo',
                'required' => true,
                'vich_file_object' => 'downloadfile',
                'vich_file_property' => 'downloadFile',
                'vich_allow_delete' => true,
                'attr' => array(
                    'data-max-size' => self::FILE_MAX_SIZE,
                    'data-max-size-error' => 'El tamaño del archivo no puede ser mayor de 2 megas'
                )
            );
    
            $formMapper
                ->add('slug', null, array('label' => 'Slug'))
                ->add('title', null, array('label' => 'Título'))
                ->add('description', null, array('label' => 'Descripción'))
                ->add('roles')
                ->add('path', 'DownloadFileAdminBundle\Form\Extension\VichFileObjectType', $fileOptions)
            ;
    
        }
    
        /**
         * @param ListMapper $listMapper
         */
        protected function configureListFields(ListMapper $listMapper)
        {
            $listMapper
                ->add('id')
                ->add('slug')
                ->add('title')
                ->add('description')
                ->add('path')
                ->add('roles')
                ->add('_action', null, array(
                    'actions' => array(
                        'show' => array(),
                        'edit' => array(),
                        'delete' => array(),
                    )
                ))
            ;
        }
    
    }
    

    这是实体,带有非持久字段和路径字段,即我要存储文件路径的位置:

    /**
         * NOTE: This is not a mapped field of entity metadata, just a simple property.
         * @Vich\UploadableField(mapping="download_file", fileNameProperty="path")
         * @var File
         */
        private $downloadFile;
    
        /**
         * @ORM\Column(type="string")
         */
        protected $path;
    
        public function getDownloadFile()
        {
            return $this->downloadFile;
        }
    
        /**
         * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $file
         *
         * @return File
         */
        public function setDownloadFile(File $file = null)
        {
            $this->downloadFile = $file;
            return $this;
        }
    
        /**
         * @return mixed
         */
        public function getPath()
        {
            return $this->path;
        }
    
        /**
         * @param mixed $path
         */
        public function setPath($path)
        {
            $this->path = $path;
        }
    

    services:
        sonata.admin.file:
            class: DownloadFileAdminBundle\Admin\DownloadFileAdmin
            arguments: [~, Opos\DownloadFileBundle\Entity\DownloadFile, SonataAdminBundle:CRUD]
            tags:
                - { name: sonata.admin, manager_type: orm, group: "Files", label: "Archivo" }
    

    和服务。yml:

    services:
        download_file_admin_bundle.vich_file_object_type:
            class: DownloadFileAdminBundle\Form\Extension\VichFileObjectType
            arguments: [ "@doctrine.orm.entity_manager" ]
            tags:
                - { name: "form.type", alias: "vich_file_object" }
    

    最后一个维希和高弗雷特配置:

    vich_uploader:
        db_driver: orm
        storage:   gaufrette
    
        mappings:
            question_image:
                uri_prefix:         ~ 
                upload_destination: questions_image_fs
                namer:              vich_uploader.namer_uniqid
            download_file:
                uri_prefix:         ~
                upload_destination: download_file_fs
                namer:              vich_uploader.namer_uniqid
    
    knp_gaufrette:
        stream_wrapper: ~
    
        adapters:
            questions_image_adapter:
                local:
                    directory: %kernel.root_dir%/../web/images/questions
            download_file_adapter:
                local:
                    directory: %kernel.root_dir%/../web/files/download
    
        filesystems:
            questions_image_fs:
                adapter:    questions_image_adapter
            download_file_fs:
                adapter:    download_file_adapter
    
    1 回复  |  直到 8 年前
        1
  •  2
  •   Vadim Ashikhman    8 年前

    因此,每当新文件对象传递给实体时,您需要更新一些特定于原则的字段值,如 updatedAt . 修改 setDownloadFile 实体的:

    /**
     * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $file
     *
     * @return File
     */
    public function setDownloadFile(File $file = null)
    {
        $this->downloadFile = $file;
    
        if ($file) {
            $this->updatedAt = new \DateTimeImmutable();
        }
    
        return $this;
    }
    

    您还需要添加 更新日期 字段和它的映射。

    请看VichUploaderBundle文档页面上的示例: https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/usage.md#step-2-link-the-upload-mapping-to-an-entity

    更新

    您还需要在上定义表单字段 downloadFile 属性而不是 path

    推荐文章