Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Vich和Gaufrete没有在sonata admin中保存文件_Php_Symfony_Sonata Admin_Vichuploaderbundle_Gaufrette - Fatal编程技术网

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

Php Vich和Gaufrete没有在sonata admin中保存文件,php,symfony,sonata-admin,vichuploaderbundle,gaufrette,Php,Symfony,Sonata Admin,Vichuploaderbundle,Gaufrette,我正在尝试使用Vich将上传链接到Sonata Admin中的实体 所有的配置都完成了,但是文件没有上传,我找不到错误 问题是,当你尝试上传文件时,一切似乎都很正常,Sonata将数据保存在所有数据库字段中,并将文件上传到sistem中的/tmp文件夹中,同时,Sonata在数据库的补丁字段中打印tmp路由。但是该文件永远不会到达Gaufrete中设置的文件夹,也不会生成唯一的名称 代码如下: 管理类: <?php namespace DownloadFileAdminBundle\Ad

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

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

问题是,当你尝试上传文件时,一切似乎都很正常,Sonata将数据保存在所有数据库字段中,并将文件上传到sistem中的/tmp文件夹中,同时,Sonata在数据库的补丁字段中打印tmp路由。但是该文件永远不会到达Gaufrete中设置的文件夹,也不会生成唯一的名称

代码如下:

管理类:

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

}
服务操作系统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" }
和服务.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

VichUploaderBundle依赖于诸如pre-persist/update之类的条令事件来尝试上传功能。当您在“管理”部分中打开现有实体并上载新文件而不更改任何其他内容时,条令不会发送生命周期事件,因为条令特定的字段均未更改

因此,每当新文件对象传递给实体时,您需要更新一些特定于条令的字段值,如
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文档页面上的示例:

更新


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

文件夹是否存在web/images/questions | web/files/download?两者都有写权限。就是这样,谢谢!
/**
 * @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;
}