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

Vich and gaufrette are not saving files in sonata admin

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

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

问题是,当你尝试上传文件时,一切似乎都正常,索纳塔将数据保存在所有数据库字段中,文件上传到系统中的 /tmp 文件夹,另外, sonata 在数据库的 patch 字段中打印 tmp 路由。但是文件永远不会到达 gaufrette 中设置的文件夹,也不会生成唯一的名称。

代码如下:

管理员Class:

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

}

这是实体,带有非持久字段和路径字段, 是我想要os删除文件路径的地方:

/**
     * 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;
    }

服务osadmin.yml

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" }

和services.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 和 gaufrette 配置:

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

VichUploaderBundle 依赖于像 pre persist/update 这样的 Doctrine Events 来触发它的上传功能。当您在管理部分打开现有实体并在不更改任何其他内容的情况下上传新文件时,学说不会分派生命周期事件,因为 none 学说特定字段已更改。

因此,无论何时将新文件对象传递给实体,您都需要更新一些特定于学说的字段值,例如 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;
}

您还需要添加 updatedAt 字段及其映射以防万一。

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

更新

您还需要在 downloadFile 属性 而不是 path

上定义表单字段