VichUploaderBundle: EmbeddedFile mapping not working in Symfony 5.4

Bug Report

Q A
BC Break no
Bundle version 1.18
Symfony version 5.4.0
PHP version 8.1.0

Summary

After upgrading to Symfony 5.4, Doctrine started complaining The class 'Vich\UploaderBundle\Entity\File' was not found in the chain configured namespaces App\Entity

Current behavior

On loading any page, Symfony throws 500 Server Error. The console reports this:

[Application] Nov 29 21:16:20 |CRITICA| REQUES Uncaught PHP Exception Doctrine\Persistence\Mapping\MappingException: "The class 'Vich\UploaderBundle\Entity\File' was not found in the chain configured namespaces App\Entity" /app/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/MappingException.php line 23 
[Application] Nov 29 21:16:20 |CRITICA| REQUES Exception thrown when handling an exception (Doctrine\Persistence\Mapping\MappingException: The class 'Vich\UploaderBundle\Entity\File' was not found in the chain configured namespaces App\Entity at /app/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/MappingException.php line 23) 
[Application] Nov 29 21:16:20 |CRITICA| PHP    Uncaught Exception: The class 'Vich\UploaderBundle\Entity\File' was not found in the chain configured namespaces App\Entity 

My Entity namespace config is:

doctrine:
    orm:
        auto_generate_proxy_classes: true
        naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                alias: App

The mapping in question is:

<?php

namespace App\Entity;

use App\Entity\Traits\CreatedAtTrait;
use App\Entity\Traits\IdTrait;
use App\Entity\Traits\UpdatedAtTrait;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Entity\File as EmbeddedFile;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
 * @ORM\MappedSuperclass
 * @Vich\Uploadable
 * @ORM\HasLifecycleCallbacks
 */
abstract class AbstractAttachment extends CrudEntity
{
    use IdTrait;
    use CreatedAtTrait;
    use UpdatedAtTrait;

    /**
     * @ORM\Embedded(class="Vich\UploaderBundle\Entity\File")
     */
    public EmbeddedFile $file;

    /**
     * @Vich\UploadableField(mapping="attachments", fileNameProperty="file.name", size="file.size", mimeType="file.mimeType", originalName="file.originalName", dimensions="file.dimensions")
     */
    protected ?File $uploadedFile = null;

    // the rest of the entity

}

Vich config looks like this:

vich_uploader:
    db_driver: orm
    storage: flysystem

    mappings:
        attachments:
            uri_prefix: '%env(resolve:USER_FILES_DOWNLOAD_URL_PREFIX)%'
            upload_destination: 'attachments.storage'
            namer: Vich\UploaderBundle\Naming\SmartUniqueNamer
            directory_namer:
                service: vich_uploader.namer_directory_property
                options:
                    property: 'directoryName'

How to reproduce

Use the lock file to install the versions I’m experiencing the issue with and run your Symfony app. It should fail immediately.

composer.lock.zip

Here is a list of updates that took place together with moving up to Symfony 5.4. I noticed Doctrine Bundle was updated as well, which might be related to the issue.

- Upgrading aws/aws-sdk-php (3.203.1 => 3.205.0)
- Upgrading doctrine/dbal (2.13.5 => 2.13.6)
- Upgrading doctrine/doctrine-bundle (2.4.3 => 2.5.0)
- Upgrading gedmo/doctrine-extensions (v3.3.0 => v3.3.1)
- Upgrading hautelook/alice-bundle (2.9.0 => dev-master ada5be3)
- Upgrading jms/metadata (2.5.2 => 2.6.1)
- Upgrading knpuniversity/oauth2-client-bundle (v2.8.0 => v2.9.0)
- Upgrading league/flysystem (2.3.1 => 2.3.2)
- Upgrading league/mime-type-detection (1.8.0 => 1.9.0)
- Upgrading ocramius/package-versions (2.3.0 => 2.4.0)
- Upgrading pdepend/pdepend (2.10.1 => 2.10.2)
- Upgrading php-http/client-common (2.4.0 => 2.5.0)
- Upgrading phpmd/phpmd (2.10.2 => 2.11.0)
- Upgrading phpoffice/phpspreadsheet (1.19.0 => 1.20.0)
- Upgrading phpunit/php-code-coverage (9.2.8 => 9.2.9)
- Upgrading psr/log (1.1.4 => 2.0.0)
- Upgrading roave/security-advisories (dev-master f08ee6e => dev-master 6f99479)
- Upgrading stof/doctrine-extensions-bundle (v1.6.0 => v1.7.0)
- Upgrading symfony/amazon-mailer (v5.3.4 => v5.4.0)
- Upgrading symfony/amqp-messenger (v5.3.7 => v5.4.0)
- Upgrading symfony/asset (v5.3.4 => v5.4.0)
- Upgrading symfony/browser-kit (v5.3.4 => v5.4.0)
- Upgrading symfony/cache (v5.3.10 => v5.4.0)
- Upgrading symfony/cache-contracts (v2.4.0 => v2.5.0)
- Upgrading symfony/config (v5.3.10 => v5.4.0)
- Upgrading symfony/console (v5.3.10 => v5.4.0)
- Upgrading symfony/crowdin-translation-provider (v5.3.0 => v5.4.0)
- Upgrading symfony/css-selector (v5.3.4 => v5.4.0)
- Upgrading symfony/debug-bundle (v5.3.4 => v5.4.0)
- Upgrading symfony/dependency-injection (v5.3.10 => v5.4.0)
- Upgrading symfony/deprecation-contracts (v2.4.0 => v2.5.0)
- Upgrading symfony/doctrine-bridge (v5.3.8 => v5.4.0)
- Upgrading symfony/doctrine-messenger (v5.3.10 => v5.4.0)
- Upgrading symfony/dom-crawler (v5.3.7 => v5.4.0)
- Upgrading symfony/dotenv (v5.3.10 => v5.4.0)
- Upgrading symfony/error-handler (v5.3.7 => v5.4.0)
- Upgrading symfony/event-dispatcher (v5.3.7 => v5.4.0)
- Upgrading symfony/event-dispatcher-contracts (v2.4.0 => v2.5.0)
- Upgrading symfony/expression-language (v5.3.7 => v5.4.0)
- Upgrading symfony/filesystem (v5.3.4 => v5.4.0)
- Upgrading symfony/finder (v5.3.7 => v5.4.0)
- Upgrading symfony/flex (v1.17.2 => v1.17.6)
- Upgrading symfony/form (v5.3.10 => v5.4.0)
- Upgrading symfony/framework-bundle (v5.3.10 => v5.4.0)
- Upgrading symfony/http-client (v5.3.10 => v5.4.0)
- Upgrading symfony/http-client-contracts (v2.4.0 => v2.5.0)
- Upgrading symfony/http-foundation (v5.3.10 => v5.4.0)
- Upgrading symfony/http-kernel (v5.3.10 => v5.4.0)
- Upgrading symfony/intl (v5.3.8 => v5.4.0)
- Upgrading symfony/lock (v5.3.10 => v5.4.0)
- Upgrading symfony/loco-translation-provider (v5.3.0 => v5.4.0)
- Upgrading symfony/mailer (v5.3.9 => v5.4.0)
- Upgrading symfony/maker-bundle (v1.36.0 => v1.36.3)
- Upgrading symfony/messenger (v5.3.10 => v5.4.0)
- Upgrading symfony/mime (v5.3.8 => v5.4.0)
- Upgrading symfony/monolog-bridge (v5.3.7 => v5.4.0)
- Upgrading symfony/notifier (v5.3.9 => v5.4.0)
- Upgrading symfony/options-resolver (v5.3.7 => v5.4.0)
- Upgrading symfony/password-hasher (v5.3.8 => v5.4.0)
- Upgrading symfony/phpunit-bridge (v5.3.10 => v5.4.0)
- Upgrading symfony/process (v5.3.7 => v5.4.0)
- Upgrading symfony/property-access (v5.3.8 => v5.4.0)
- Upgrading symfony/property-info (v5.3.8 => v5.4.0)
- Upgrading symfony/rate-limiter (v5.3.4 => v5.4.0)
- Upgrading symfony/redis-messenger (v5.3.10 => v5.4.0)
- Upgrading symfony/routing (v5.3.7 => v5.4.0)
- Upgrading symfony/runtime (v5.3.10 => v5.4.0)
- Upgrading symfony/security-bundle (v5.3.8 => v5.4.0)
- Upgrading symfony/security-core (v5.3.10 => v5.4.0)
- Upgrading symfony/security-csrf (v5.3.4 => v5.4.0)
- Upgrading symfony/security-guard (v5.3.7 => v5.4.0)
- Upgrading symfony/security-http (v5.3.10 => v5.4.0)
- Upgrading symfony/serializer (v5.3.10 => v5.4.0)
- Upgrading symfony/service-contracts (v2.4.0 => v2.5.0)
- Upgrading symfony/stopwatch (v5.3.4 => v5.4.0)
- Upgrading symfony/string (v5.3.10 => v5.4.0)
- Upgrading symfony/translation (v5.3.10 => v5.4.0)
- Upgrading symfony/translation-contracts (v2.4.0 => v2.5.0)
- Upgrading symfony/twig-bridge (v5.3.7 => v5.4.0)
- Upgrading symfony/twig-bundle (v5.3.10 => v5.4.0)
- Upgrading symfony/uid (v5.3.10 => v5.4.0)
- Upgrading symfony/validator (v5.3.10 => v5.4.0)
- Upgrading symfony/var-dumper (v5.3.10 => v5.4.0)
- Upgrading symfony/var-exporter (v5.3.8 => v5.4.0)
- Upgrading symfony/web-link (v5.3.4 => v5.4.0)
- Upgrading symfony/web-profiler-bundle (v5.3.8 => v5.4.0)
- Upgrading symfony/webpack-encore-bundle (v1.12.0 => v1.13.1)
- Upgrading symfony/workflow (v5.3.4 => v5.4.0)
- Upgrading symfony/yaml (v5.3.6 => v5.4.0)
- Upgrading theofidry/alice-data-fixtures (1.4.0 => dev-master 9e94af9)
- Upgrading twig/cssinliner-extra (v3.3.3 => v3.3.4)
- Upgrading twig/extra-bundle (v3.3.3 => v3.3.4)
- Upgrading twig/html-extra (v3.3.3 => v3.3.4)
- Upgrading twig/inky-extra (v3.3.3 => v3.3.4)
- Upgrading twig/intl-extra (v3.3.3 => v3.3.4)
- Upgrading twig/markdown-extra (v3.3.3 => v3.3.4)
- Upgrading twig/twig (v3.3.3 => v3.3.4)

Expected behavior

I would think the bundle should work on 5.4.x without any trouble, if it worked on 5.3.x.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 20 (4 by maintainers)

Most upvoted comments

A quick fix/workaround that works for me, is adding the necessary mapping information into the config/packages/doctrine.yaml manually:

doctrine:
  ...
  orm:
    ...
    mappings:
      ...
      Vich\UploaderBundle:
        is_bundle: false
        type: annotation
        dir: '%kernel.project_dir%/vendor/vich/uploader-bundle/src/Entity'
        prefix: 'Vich\UploaderBundle\Entity'
        alias: Vich\UploaderBundle

@garak Do you plan to release a new version of your bundle with this fix?

Not before we add support for Symfony 6

If someone is interested, I had to manually force symfony/doctrine-bridge to be in 5.3.* in the composer.json in order to not have the issue The class 'Vich\UploaderBundle\Entity\File' was not found in the chain configured namespaces

Fixed in #1244

You should try using ^1.19@dev

@garak There is no change for me when using that version…