首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Vich和gaufrette没有保存奏鸣曲管理中的文件。

Vich和gaufrette没有保存奏鸣曲管理中的文件。
EN

Stack Overflow用户
提问于 2017-10-18 14:25:11
回答 1查看 167关注 0票数 1

我试图做一个上传链接到一个实体在奏鸣曲行政使用Vich。

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

问题是,当y试图上传文件时,每件事情似乎都很好,Sonata将数据保存在所有数据库字段中,该文件被上传到数据库中的/tmp文件夹中,奏鸣曲还在数据库中的修补字段中打印tmp路径。但是,该文件永远不会到达在gaufrette中设置的文件夹,也不会生成唯一的名称。

以下是代码:

管理类:

代码语言:javascript
复制
<?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(),
                )
            ))
        ;
    }

}

这是一个实体,具有非持久字段和path字段,它是eI希望存储文件路径的地方:

代码语言:javascript
复制
/**
     * 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

代码语言:javascript
复制
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:

代码语言:javascript
复制
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配置:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-18 16:02:41

VichUploaderBundle依赖于Doctrine事件(如预持久化/更新)来跟踪它的上传功能。当您打开管理部分中的现有实体并上传新文件而不更改任何其他内容时,原则不会分派生命周期事件,因为任何特定的原则字段都不会被更改。

因此,每当新的文件对象传递给实体时,您都需要更新一些特定于理论的字段值,比如updatedAt。修改实体的setDownloadFile以:

代码语言:javascript
复制
/**
 * @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上定义表单字段。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46812528

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档