Reputation: 3678
I am using current versions of Symfony (3.2.3) and Doctrine ORM (2.5.6) and I'm still trying to wrap my head around how to handle the deprecation of getEntityManager()
and related methods correctly.
For instance, the current documentation states that this is the way to create a query builder instance:
$repository = $this->getDoctrine()->getRepository('AppBundle:Product');
$queryBuilder = $repository->createQueryBuilder('p')
But the way I see it (and my IDE seems to agree), getRepository()
is type hinted to return an instance of ObjectRepository
and the method createQueryBuilder()
is not declared in that interface but in the implementing class EntityRepository
.
An other example would be explicitly starting a transaction in a controller:
$this->getDoctrine()->getEntityManager()->getConnection()->beginTransaction()
I'm supposed to call getManager()
instead of getEntityManager()
because the latter method is deprecated. But getManager()
is type hinted to return an instance of ObjectManager
which does not declare getConnection()
.
PHP being PHP the code still seems to be working in all cases, but my IDE and I are left with an uneasy feeling. So, what is the correct way to create a query builder or begin a transaction in Symfony 3? Am I missing something?
Upvotes: 6
Views: 21763
Reputation: 2743
Doctrine has a two types of Data Mappers: the ORM for the RDBMS and the ODM for document-oriented DBs (mostly for MongoDB). Data mappers try to achieve in a sort of persistence-ignorance and both of them implements a generic interfaces defined in the Doctrine\Common
, such as ObjectManager
or ObjectRepository
.
In the Symfony the getDoctrine()
method returns an instance of Registry
class from the DoctrineBundle
(by default). In short this service holds all available connections and entity managers. His method getManager()
implemented in the Doctrine\Common
, it don't knows what type of data mapper and declares return type as generic ObjectManager
.
To avoid IDE warnings you can explicitly define the inline PHPDoc. Netbeans and PHPStorm are support it.
/* @var $entityManager Doctrine\ORM\EntityManager */
$entityManager = $this->getDoctrine()->getManager();
Other solution is a simple helper method:
public function getEntityManager(): EntityManager
{
return $this->getDoctrine()->getManager();
}
With above solutions we assume that requested service is actually from the ORM, so is a sort of type downcasting: actual class will be checked only in runtime. For the type-safety you can directly inject the entity manager in the your service.
Upvotes: 12
Reputation: 689
If your problem is only your IDE, you can tell it that the return isn't an ObjectRepository with this, for example:
/** @var ProductRepository $repository */
$repository = $this->getDoctrine()->getRepository('AppBundle:Product');
$queryBuilder = $repository->createQueryBuilder('p');
Because you know that that's what you'll get.
Upvotes: 0
Reputation: 319
try this:
$queryBuilder = $this->getDoctrine()->getManager()->getRepository('AppBundle:Product')->createQueryBuilder('p'); and update your symfony plugin if you are using phpstorm or ignore your ide
Upvotes: -2