0

我尝试仅使用 YML 配置 Vich 并遇到问题。我创建声明文件(路径:src/My/GreatBundle/Resources/config/vich_uploader/Media.yml):

My\GreatBundle\Entity\Media:
    image:
        mapping: image_mapping
        filename_property: file_name

创建实体媒体(路径:src/My/GreatBundle/Entity/Media.php):

<?php
namespace My\GreatBundle\Entity;

use Symfony\Component\HttpFoundation\File\File;

class Media{
    private $id;
    public function getId(){
        return $this->id;
    }

    protected $file_name;
    protected $image;

    public function setFileName($fileName){
        $this->file_name = $fileName;

        return $this;
    }

    public function getFileName(){
        return $this->file_name;
    }

    public function setImage(File $image){
        $this->image = $image;

        return $this;
    }

    public function getImage(){
        return $this->image;
    }
}

最后创建表单(路径:src/My/GreatBundle/Entity/Form/Type/MediaType.php):

<?php
namespace My\GreatBundle\Entity\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

use Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader;

use My\GreatBundle\Entity\Media;

use Vich\UploaderBundle\Form\Type\VichImageType;

class MediaType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options){
        $builder
            ->add('image', VichImageType::class, [
                'required' => false,
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver){
        $resolver->setDefaults(array(
             'data_class' => Media::class,
        ));
    }

    public function getName(){
        return 'my_great_type_media';
    }
}

一个想法是什么小姐?我有这个消息:

The class "My\GreatBundle\Entity\Media" is not uploadable. If you use annotations to configure VichUploaderBundle, you probably just forgot to add `@Vich\Uploadable` on top of your entity. If you don't use annotations, check that the configuration files are in the right place. In both cases, clearing the cache can also solve the issue.

显然我有明确的缓存。

编辑 1:config.yml

vich_uploader:
    db_driver: orm
    mappings:
        image:
            uri_prefix: /media
            upload_destination: '%kernel.root_dir%/../uploads/media'
            delete_on_remove: true
            delete_on_update: false
            inject_on_load: false
            namer:
                service: vich_uploader.namer_property
                options: { property: 'id' }
4

1 回答 1

0

更新:

如我所见,在您的配置中,您的 filename_property 称为file_name,但您的表单使用属性图像作为文件名属性。

此外,在您的Media.yml文件中,您使用名为image_mapping的映射名称,但在config.yml您的映射中名为image

我不知道您避免注释的原因,但是当您使用它们时,生活会变得更加轻松:)

有一个相关的问题,所以检查How to configure VichUploader in entity mapped by yml?

于 2017-07-24T06:49:49.757 回答