How to fix Unable to find "Proxies\__CG__\App\Entity\*" entity identifier associated with the UnitOfWork

How to fix Unable to find "Proxies\__CG__\App\Entity\*" entity identifier associated with the UnitOfWork banner

I was testing a Symfony command today and I got the following unexpected error

08:51:22 CRITICAL  [console] Error thrown while running command "app:* --env=prod". Message: "Unable to find "Proxies\__CG__\App\Entity\*" entity identifier associated with the UnitOfWork" ["exception" => Doctrine\ORM\EntityNotFoundException^ { …},"command" => "app:* --env=prod","message" => "Unable to find "Proxies\__CG__\App\Entity\*" entity identifier associated with the UnitOfWork"]

In EntityNotFoundException.php line 43:

  Unable to find "Proxies\__CG__\App\Entity\*" entity identifier associated with the UnitOfWork

How to fix this error?

It turns out that using EntityManager#clear() in my script caused that issue. Check your code and see if you are using clear() correctly.

What is EntityManager#clear() used for?

clear() is used to detach all entities from the entity manager.

Detaching entities is useful if you ever need to process large amounts of data coming from Doctrine. Doctrine keeps an object for every entity it processes, which means that at some point (let's say you iterate over 1 million rows) the memory will run out and your script will crash with a memory error.

Learn more about Batch Processing in Doctrine.