0

I try to configure Vich using only YML and face to a problem. I create declaration file (path: src/My/GreatBundle/Resources/config/vich_uploader/Media.yml) :

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

Create the entity media (path: 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;
    }
}

And finally create form (path: 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';
    }
}

An idea what is miss ? I have this message :

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.

Obviously I have clear cache.

Edit 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' }
Draeli
  • 151
  • 2
  • 13

1 Answers1

0

UPDATED:

As I can see, in your config stands that your filename_property is called file_name, but your form is using property image as filename property.

Also, in your Media.yml file you are using mapping name called image_mapping but in config.yml your mapping is named image.

I don't know the reason why you are avoiding annotations, but the life gets much easier when you use them :)

There is a related issue to this, so check How to configure VichUploader in entity mapped by yml?

Branimir Đurek
  • 632
  • 5
  • 13
  • I try to write invalid syntax but yml seems not recognized ! :( I prefer YML for clarity when I use, because reading Annotation is like reading alien language, and as I began with yml, I think it's more consistent to continue in yml. – Draeli Jul 24 '17 at 15:31