Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getManager causes an error, getEntityManager works fine?

As a newbie in Symfony2 & doctrine i'm struggling (in a custom repository class) with the difference between getEntityManager() and getManager().

I know getEntityManager() is being deprecated but if I use getManager() instead, I'm getting "Undefined method 'getManager'. The method name must start with either findBy or findOneBy!"

In my class, the following code works:

public function haalidop($verbid)
{
    return $this->getEntityManager()
                ->createQuery('SELECT p FROM myBundle:Verbs p WHERE p.verbid='.$verbid)
                ->getSingleResult();
}

If I change (upgrade?) it like the code below, I get the error...

public function haalidop($verbid)
    {
        return $this->getManager()
                    ->createQuery('SELECT p FROM myBundle:Verbs p WHERE p.verbid='.$verbid)
                    ->getSingleResult();
    }

Anyone any suggestion what's wrong here?

like image 594
PBR Avatar asked Feb 12 '14 22:02

PBR


1 Answers

The getEntityManager method of the Registry is deprecated. Since you're in a Repository, you extended not the Registry but the EntityRepository. That class has only a getEntityManager method, which isn't deprecated.

The reason for this inconsistency is quite easy: The Registry is something that's used for other Doctrine library too, like their ODMs. They don't use the name "Entity", but "Document". For that reason, using getEntityManager didn't make much sense for ODMs, that's why they changed if to getManager.
On the other hand, the EntityRepository -as its name already tells us- is ORM specific, meaning there is no confusing for ODM users (they use another repository class).

like image 105
Wouter J Avatar answered Sep 28 '22 09:09

Wouter J