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

时间:2017-10-18 14:25:11

标签: php symfony sonata-admin vichuploaderbundle gaufrette

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

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

问题是,当你试图上传文件时,每件事似乎都能正常工作,Sonata会在所有数据库字段中保存数据,并且文件会上传到teh sistem中的/ tmp文件夹,也就是奏鸣曲打印数据库中补丁字段中的tmp路由。但是文件永远不会到达gaufrette中设置的文件夹,也不会生成唯一的名称。

以下是代码:

管理员类:

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

}

这是实体,具有非持久性fieln和路径字段,女巫是我想要存储文件路径:

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

服务os admin.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

1 个答案:

答案 0 :(得分:2)

VichUploaderBundle依赖于像pre persist / update这样的Doctrine事件来篡改其上传功能。当您在管理部分中打开现有实体并上传新文件而不更改任何其他内容时,doctrine将不会调度生命周期事件,因为没有更改任何特定于学说的字段。

因此,每当新文件对象传递给实体时,您需要更新某些特定于学说的字段值,例如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

上定义表单字段