Search code examples
phpsymfonycachingdoctrine-orm

MappingException: The target-entity cannot be found after renaming class. Caching issue on production environment


I'm using Symfony 4.4 with doctrine 2.0.8 I've recently been refactoring a lot of our code and some of that involves renaming certain class names. This all works perfectly fine until we deploy to our production environment, where I'm getting MappingExceptions because it keeps trying to find old class names.

E.g.

Relationship in the namespace App\Entity\PortalUser has been renamed/moved to App\Entity and renamed to PortalUserRelationship.php.

Locally all of this work!

But once I try to bin/console cache:clear --env=prod I get the following error:

The target-entity App\Entity\PortalUser\Relationship cannot be found in 'App\Entity\PortalUser#portalUserRelationships'.

Of course this makes sense because this doesn't exist anymore at that location but somehow doctrine is still looking for the Entity in the wrong place?

This is the relation code just to prove that it's all correct.

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\PortalUserRelationship", mappedBy="portalUser", orphanRemoval=true)
     */
    private Collection $portalUserRelationships;

So somewhere there's something making it impossible to clear the production cache and I have found no way on how to clear this cache.

Mind you bin/console clear:cache --env=dev works, but bin/console clear:cache --env=prod fails.

I have tried the following to no avail.

php bin/console doctrine:cache:clear-metadata
php bin/console doctrine:cache:clear-query
php bin/console doctrine:cache:clear-result

I've also tried removing the entire var/cache folder, doesn't help.

I've tried restarting the apache2 server, nope.

What am I missing here?


Solution

  • Adding the following to doctrine.yml for the failing environments fixed the issue.

    doctrine:
        orm:
            auto_generate_proxy_classes: true