The error of an individual object in the Doctrine

I am sending an array of entities to the controller, all of which I want to delete. However, the code below causes an error A detached entity was found during removed MyProject\Bundle\MyBundle\Entity\MyEntity@000000004249c13f00000001720a4b59. Where am I mistaken?

$doctrineManager = $this->getDoctrine()->getManager();
foreach ($form->getData()->getEntities() as $entity) {
    $doctrineManager->merge($entity);  
    $doctrineManager->remove($entity);
}
$doctrineManager->flush();
+4
source share
1 answer

You must use the merge operation for objects in the disconnected state, and you want to put them in a managed state.

The merger should be performed as follows $entity = $em->merge($detachedEntity). It then $entityrefers to the fully managed copy returned by the merge operation. Therefore, if your $formcontains individual elements, you should configure your code as follows:

$doctrineManager = $this->getDoctrine()->getManager();
foreach ($form->getData()->getEntities() as $detachedEntity) {
    $entity = $doctrineManager->merge($detachedEntity);  
    $doctrineManager->remove($entity);
}
$doctrineManager->flush();

, $form , , :

$doctrineManager = $this->getDoctrine()->getManager();
foreach ($form->getData()->getEntities() as $entity) {
    $doctrineManager->remove($entity);
}
$doctrineManager->flush();

. Java Persistence API, Doctrine2 .

JPA state transitions

+19

Source: https://habr.com/ru/post/1539180/


All Articles