PBR
PBR

Reputation: 316

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?

Upvotes: 7

Views: 7718

Answers (4)

import { DataSource } from 'typeorm'
import { InjectDataSource } from "@nestjs/typeorm"; 

export class TenantsConsumer {

    constructor(
         @InjectDataSource() private readonly datasource : DataSource
    ){}

    public async promote(tenant: Tenanats){
       await this.datasource.query(
             `CREATE SCHEMA IF NOT EXISTS "${tenant.db}"`
           );
    }
}

Upvotes: 0

ferdynator
ferdynator

Reputation: 6410

The getEntityManager method is still valid in repositories as you can see in the current docs. It is deprecated in Controllers though and the getManager method should be used instead.

Upvotes: 1

Wouter J
Wouter J

Reputation: 41954

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).

Upvotes: 12

Derick F
Derick F

Reputation: 2769

if in a controller:

$this->get('doctrine')->getManager();

if in a service:

$this->container->get('doctrine')->getManager();

Upvotes: -1

Related Questions