dustin10 / VichUploaderBundle

A simple Symfony bundle to ease file uploads with ORM entities and ODM documents.
MIT License
1.85k stars 521 forks source link

Cannot create mapping for YML declaration #861

Closed indapublic closed 6 years ago

indapublic commented 6 years ago
Q A
Bug report? yes/no
Feature request? yes/no
BC Break report? yes/no
Symfony version 3.4
VichUploaderBundle version 1.8

I have entity AdminBundle\Entity\TuningPart\TuningPartImage which described in YML format.

Want to use Vich Uploader for Easyadmin

Tried use this filename _Resources/config/vichuploader/TuningPart.TuningPartImage.yml and _Resources/config/vichuploader/TuningPartImage.yml. No difference. Still receiving error Mapping not found for field "imageFile". Tried to create incorrect YML but there is no errors. Seems file hasn't been imported.

garak commented 6 years ago

Try to force directories in your config

Gemorroj commented 6 years ago

@garak can you show example? current documentation (https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/mapping/yaml.md) seems outdated for symfony 4.

garak commented 6 years ago

While examples are more old-Symfony oriented, you can use them with Symfony 4 as well. Just adapt your paths

Gemorroj commented 6 years ago

An exception has been thrown during the rendering of a template ("The class "App\Entity\File" 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.").

src/Resource/config/vich_uploader/Entity.File.yml:

App\Entity\File:
    file:
        mapping:           file
        filename_property: fileName
garak commented 6 years ago

Be aware that "clearing the cache" means manually deleting all your cache files/dirs. Then, you can try to check if your entities are uploadable using console commands provided by this bundle. Did you force directories option?

Gemorroj commented 6 years ago

see Vich\UploaderBundle\DependencyInjection\VichUploaderExtension::registerMetadataDirectories and Metadata\Driver\FileLocator::findFileForClass (part of https://github.com/schmittjoh/metadata).

working config:

config/vich_uploader/Entity.File.yml

App\Entity\File:
    file:
        mapping:           file
        filename_property: fileName

config/packages/vich_uploader.yaml

vich_uploader:
    db_driver: orm
    mappings:
        file:
            namer: Vich\UploaderBundle\Naming\UniqidNamer
            # ... my config
    metadata:
        auto_detection: false
        directories:
            - {path: '%kernel.project_dir%/config/vich_uploader', namespace_prefix: 'App'}
Gemorroj commented 6 years ago

But to be honest, with this inadequately complicated configuration you need to do something. Because the bundle is unreasonably difficult to configure.

garak commented 6 years ago

Feel free to propose a more reasonable solution, if you can find one.

indapublic commented 6 years ago

Try to force directories - no success

screen shot 2018-04-15 at 01 18 55 screen shot 2018-04-15 at 01 19 00
garak commented 6 years ago

Please provide the content of your mapping file. And remember:

you can try to check if your entities are uploadable using console commands provided by this bundle

indapublic commented 6 years ago

As I said before, i create mapping file with invalid YML (just for checking) and I cannot see any errors, so I think that file hasn't been imported.

vich_uploader.yml (from config)

vich_uploader:
    db_driver:  orm
#    templating: true
#    twig:       true
#    form:       true
#    storage:    file_system
    mappings:
        tuning_part_image:
            uri_prefix: '%app.path.tuning_part_images%'
            upload_destination: '%kernel.root_dir%/../web/uploads/images/tuning_parts'
            inject_on_load:   false
            delete_on_update: true
            delete_on_remove: true
    metadata:
        auto_detection: false
        directories:
            - { path: '%kernel.root_dir%/vich_uploader', namespace_prefix: 'Admin' }
indapublic commented 6 years ago

Hm.... I not see any vich commands in bin/console. Just checked in AppKernel, bundle has been included. Any thoughts?

Gemorroj commented 6 years ago

+ @indapublic I don't see any console commands either

indapublic commented 6 years ago

@Gemorroj What are your versions of Symfony and Vich?

Gemorroj commented 6 years ago

@indapublic Symfony 4.0.8, Vich 1.8.2

garak commented 6 years ago

Are you using flex? Anyway, Symfony4 should use config/bundles.php instead of AppKernel

Gemorroj commented 6 years ago

@garak yes, flex. config/bundles.php have Vich\UploaderBundle\VichUploaderBundle::class => ['all' => true], but, php bin/console list does not display vich commands

indapublic commented 6 years ago

@garak I'm not.

garak commented 6 years ago

You can try with latest dev-master (or 1.8.*@dev), commands should appear now.

indapublic commented 6 years ago

Commands are appeared, thank you @garak

indapublic commented 6 years ago

bin/console vich:mapping:list-classes

In FileLocator.php line 51:

RecursiveDirectoryIterator::__construct(/Volumes/Data/www/website/admin/app/vich_uploader): failed to open dir: No such file or directory

I'm not sure why it looking for app/vich_uploader

Gemorroj commented 6 years ago

@garak now with console commands everything is fine

garak commented 6 years ago

@indapublic you configured path: '%kernel.root_dir%/vich_uploader', so it's your fault.

indapublic commented 6 years ago

@garak Agreed, lost control with tests. Sorry. Yes, my class is attached.

bin/console vich:mapping:list-classes Looking for uploadable classes. Found Admin\TuningPart\TuningPartImage Found 1 classes. NOTE: Only classes configured using XML or YAML are displayed.

So. Bundle can see my YML in vich_uploader. YML contains invalid data

screen shot 2018-04-16 at 00 46 18

But no errors at the moment

garak commented 6 years ago

Your YAML is not strictly invalid, but I see it does not contains useful mapping informations. Why didn't you try with a useful one?

indapublic commented 6 years ago

Trying with this...

AdminBundle\TuningPart\TuningPartImage:
    imageFile:
        mapping:           tuning_part_image
        filename_property: imageFileName

bin/console vich:mapping:list-classes Looking for uploadable classes. Found Admin\TuningPart\TuningPartImage Found 1 classes. NOTE: Only classes configured using XML or YAML are displayed.

bin/console vich:mapping:debug tuning_part_image Debug information for mapping tuning_part_image uri_prefix: '/uploads/images/tuning_parts' upload_destination: '/Volumes/Data/www/ru.compositdv/admin/app/../web/uploads/images/tuning_parts' inject_on_load: false delete_on_update: true delete_on_remove: true namer: array ( 'service' => NULL, 'options' => NULL, ) directory_namer: array ( 'service' => NULL, 'options' => NULL, ) db_driver: 'orm'

Easy admin configuration:

...
- { property: 'imageFile', type: 'vich_file', base_path: '%app.path.tuning_part_images%' }
...

Still receiving Mapping not found for field "imageFile"

screen shot 2018-04-16 at 00 53 03
garak commented 6 years ago

bin/console vich:mapping:debug-class "Admin\TuningPart\TuningPartImage"

indapublic commented 6 years ago

Thanks, I'm found that I'm using wrong namespace in vich_uploader.yml. Fixed.

- { path: '%kernel.root_dir%/config/vich_uploader', namespace_prefix: 'AdminBundle' }

Running your command (with correct bundle namespace):

bin/console vich:mapping:debug-class "AdminBundle\TuningPart\TuningPartImage" Introspecting class AdminBundle\TuningPart\TuningPartImage:

indapublic commented 6 years ago

But still not working in Easy Admin :(

indapublic commented 6 years ago

Rework project in annotation instead of yml and all works fine. If I removed annotation from property

@Vich\UploadableField(mapping="tuning_part_image", fileNameProperty="imageFileName")

I receive same error "Mapping Not Found"

Cannot find what's wrong in YML project. I'll close topic.