0

I have read the the other questions concerning this issue but have not come across a solution as of yet.

I get the following error message:

The class 'Doctrine\ORM\EntityManager' was not found in the chain configured >namespaces ZfcUser\Entity, Common\Entity, Employment\Entity, Intern\Entity, >Team\Entity, PurchaseRequest\Entity.

I have a HolidayEntity, HolidayController, HolidayService.

Adding a holiday works, but when I try to remove a holiday the error pops up. I pass the holiday id from to controller to the service, which then fetches the associated object and runs the doctrine 2 removeEntity command.

I am unsure as to how to tackle the problem at the moment.

Controller code:

public function deleteAction()
{
    $holidayId = $this->params()->fromRoute('id', 0);
    try {  
        $this->getServiceLocator()->get('holidayService')->removeHoliday($holidayId);
    } catch (Exception $e) {
        $this->flashMessenger()->addErrorMessage($e->getMessage() . '<br>' . $e->getTraceAsString());
    }
    return $this->redirect()->toRoute('holiday/list');
}

Service Code:

public function removeHoliday($holidayId)
{
    try{
    $holiday = $this->findOneHolidayById($holidayId);
    $this->removeEntity($holiday);
    } catch (Exception $e) {
        var_dump($e);
    }
}

protected function removeEntity($entity)
{
    $this->getEntityManager()->remove($entity);
    $this->getEntityManager()->flush();
}

The code breaks in the "$this->getEntityManager()->remove($entity)" method.

4
  • 1
    what is passed as $entity? Commented Jun 4, 2015 at 13:59
  • 1
    can you post the code for the method findOneHolidayById ? are you sure the id passed from the route exists?
    – Matteo
    Commented Jun 4, 2015 at 15:13
  • The id exists, findOneHolidayById returns a holiday object when the id is passed into it. So that method cannot be the issue.
    – eGlu
    Commented Jun 4, 2015 at 22:19
  • I overlooked the var_dump details as being a entitymanger entity and not as a holiday entity.
    – eGlu
    Commented Jun 5, 2015 at 10:27

2 Answers 2

16

The error you are getting relates to Doctrine not being able to find an Entity called Doctrine\ORM\EntityManager, which is clearly wrong.

My guess is that somewhere (perhaps in the getEntityManager() method) you are passing an instance of the entity manager to the entity manager.

For example

$entityManager = new EntityManager();
$entity = new EntityManager();
$entityManager->remove($entity);
1
  • That guess was spot on. @Matteo, you were on the right track, the id passed was right and the return from the method was wrong since i was returning the entitymanager instead of the result object.
    – eGlu
    Commented Jun 5, 2015 at 10:26
0

@AlexP's answer is correct. To be specific, this is one of common mistakes when using doctrine:

Repository.php

public function findBySomething($something)
{
    return $this->createQueryBuilder('e')
       ->where('e.something = :something')
       ->setParameter('something', $something);
}

Somewhere else

// WRONG USE CASE

$entity = $this->repository->findBySomething('foo');
$this->entityManager->remove($entity);

Correction

$entity = $this->repository->findBySomething('foo')
    ->getQuery()
    ->getFirstResult();
$this->entityManager->remove($entity);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.