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”中。

有什么提示吗?提前致谢!

4

2 回答 2

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();
    }
}
于 2016-05-31T13:12:51.137 回答
0

也许你忘记了课堂上的use陈述Vich\UploaderBundle\Mapping\PropertyMapping

于 2016-05-31T09:29:23.913 回答