Entity Manager return repository with old entity manager after entity manager reset #5972

Closed
opened 2026-01-22 15:23:47 +01:00 by admin · 1 comment
Owner

Originally created by @DangerLifter on GitHub (May 26, 2018).

Originally assigned to: @Majkl578 on GitHub.

Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Symfony version 4.0.4
Version 2.6.1

Initially posted here: https://github.com/symfony/symfony/issues/26503

There are entities Product and Category with Many-to-One relation. In controller multiple products added in a loop. Each product is assigned to same category, which is loaded before loop. Product has unique field in database, so in loop "UniqueConstraintViolationException" is caught and loop going further with next product. Because exceptions causes entity manager to be closed there is code in cycle to reset manager if need it:

if (!$em->isOpen()) {
    $em = EntityManager::create(
        $em->getConnection(), $em->getConfiguration()
    );
    // have to reload entity after recreate entity manager
    if ($category) {
        //$category = $em->getRepository(Category::class)->find($category->getId());
        //$category = $em->find(Category::class, $category->getId());
    }
}

As category loaded once before loop it needs to be reloaded for new entity manager. If
$category = $em->getRepository(Category::class)->find($category->getId()); is used it does not work and causes

Product "" error: A new entity was found through the relationship 'App\Entity\Product#category' that was not configured to cascade persist operations for entity: App\Entity\Category@00000000380b58f9000000004390b330. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem implement 'App\Entity\Category#__toString()' to get a clue..

But it correctly works with usage
$category = $em->find(Category::class, $category->getId());

As I found it happens because $em->getRepository(Category::class) returns repository with old instance of entity manager.

Originally created by @DangerLifter on GitHub (May 26, 2018). Originally assigned to: @Majkl578 on GitHub. | Q | A | ---------------- | ----- | Bug report? | yes | Feature request? | no | BC Break report? | no | RFC? | no | Symfony version | 4.0.4 | Version | 2.6.1 Initially posted here: https://github.com/symfony/symfony/issues/26503 There are entities Product and Category with Many-to-One relation. In controller multiple products added in a loop. Each product is assigned to same category, which is loaded before loop. Product has unique field in database, so in loop "UniqueConstraintViolationException" is caught and loop going further with next product. Because exceptions causes entity manager to be closed there is code in cycle to reset manager if need it: ```php if (!$em->isOpen()) { $em = EntityManager::create( $em->getConnection(), $em->getConfiguration() ); // have to reload entity after recreate entity manager if ($category) { //$category = $em->getRepository(Category::class)->find($category->getId()); //$category = $em->find(Category::class, $category->getId()); } } ``` As category loaded once before loop it needs to be reloaded for new entity manager. If `$category = $em->getRepository(Category::class)->find($category->getId());` is used it does not work and causes > Product "" error: A new entity was found through the relationship 'App\Entity\Product#category' that was not configured to cascade persist operations for entity: App\Entity\Category@00000000380b58f9000000004390b330. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem implement 'App\Entity\Category#__toString()' to get a clue.. But it correctly works with usage `$category = $em->find(Category::class, $category->getId());` As I found it happens because $em->getRepository(Category::class) returns repository with old instance of entity manager.
admin added the BugInvalid labels 2026-01-22 15:23:47 +01:00
admin closed this issue 2026-01-22 15:23:48 +01:00
Author
Owner

@Majkl578 commented on GitHub (May 26, 2018):

Doctrine's EntityManager has no concept of resettability - once closed (i.e. by exception), it can no longer be used at all, neither can be used i.e. UnitOfWork. Any entity managed by the closed EntityManager becomes invalid (unmanaged) too.
Hence this looks like a wrong assumption in your code (i.e. expecting entity to outlive closed Entity Manager).
Hint: you can use EntityManager::getReference() to get a managed placeholder of your object, but since it does not query the DB to check for its existence, you must ensure it's valid (i.e. not deleted in meantime).
Closing as invalid for the reason above.

@Majkl578 commented on GitHub (May 26, 2018): Doctrine's EntityManager has no concept of resettability - once closed (i.e. by exception), it can no longer be used at all, neither can be used i.e. UnitOfWork. Any entity managed by the closed EntityManager becomes invalid (unmanaged) too. Hence this looks like a wrong assumption in your code (i.e. expecting entity to outlive closed Entity Manager). Hint: you can use `EntityManager::getReference()` to get a managed placeholder of your object, but since it does not query the DB to check for its existence, you must ensure it's valid (i.e. not deleted in meantime). Closing as invalid for the reason above.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5972