首页 文章

Symfony / VichUploaderBundle:DirectoryNamer

提问于
浏览
0

我正在尝试实现DirectoryNamer,但不幸的是,这会导致错误消息,我不知道下一步该怎么做 .

控制器(摘录):

if ($form->isSubmitted() && $form->isValid()) {

        $em = $this->getDoctrine()->getManager();
        $em->persist($image);
        $em->flush();
        //...
}

DirectoryNamer服务类:

use Vich\UploaderBundle\Naming\DirectoryNamerInterface;

class ArtistDirectoryNamer implements DirectoryNamerInterface
{
    public function directoryName($image, PropertyMapping $mapping) {   
        return $image->getArtist()->getId();
    }
}

VichUploader配置:

vich_uploader:
db_driver: orm
mappings:
    upload_artists:
        uri_prefix:         /upload/artists
        upload_destination: %kernel.root_dir%/../web/upload/artists
        directory_namer:    macms_admin.artist_directory_namer
        namer:              vich_uploader.namer_uniqid
        inject_on_load:     false
        delete_on_update:   true
        delete_on_remove:   true

...以及由此产生的错误消息:

编译错误:ArtistDirectoryNamer :: directoryName()的声明必须与Vich \ UploaderBundle \ Naming \ DirectoryNamerInterface :: directoryName兼容($ object,Vich \ UploaderBundle \ Mapping \ PropertyMapping $ mapping)

我的代码出了什么问题?在我实现DirectoryNamer之前,上传工作正常,文件已存储在'/ upload / artists'中 .

任何提示?提前致谢!

2 回答

  • 0

    也许你忘记了 Vich\UploaderBundle\Mapping\PropertyMapping 类的 use 语句?

  • 0

    由于K-Phoen - 这是工作解决方案:

    namespace Acme\Project\AdminBundle\Services;
    
    use Vich\UploaderBundle\Naming\DirectoryNamerInterface;
    use Vich\UploaderBundle\Mapping\PropertyMapping;
    
    class ArtistDirectoryNamer implements DirectoryNamerInterface
    {
        public function directoryName($image, PropertyMapping $mapping) {   
            return $image->getArtist()->getId();
        }
    }
    

相关问题