deleteAction() with Doctrine

Home Forums Learn ZF2 deleteAction() with Doctrine

This topic contains 1 reply, has 2 voices, and was last updated by  Slavey Karadzhov 3 years ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #299

    Hi, i’ve been tried to delete one entity with the code bellow:

    $id = $this->params(‘id’);
    if(!$id) {
    return $this->redirect()->toRoute(‘user/default’, array(
    ‘controller’ => ‘account’,
    ‘action’ => ‘view’,
    ));
    }

    $entityManager = $this->serviceLocator->get(‘entity-manager’);
    $userEntity = $this->serviceLocator->get(‘user-entity’);
    $userEntity->setId($id);
    $entityManager->remove($userEntity);
    $entityManager->flush();

    But it shows the message and don’t remove the entity from database:
    A detached entity was found during removed User\Model\Entity\User@0000000036df71400000000077cf16f7

    #300

    Slavey Karadzhov
    Participant

    Hi Daniel,
    Replace the line:

    $userEntity->setId($id);
    with that one:
    $userEntity = $entityManager->find(get_class($userEntity), $id);

    that should work and give you better error reporting.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.