You have injected the EntityManager via parameter $entityManager. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.
The EntityManager might become unusable for example if a transaction is
rolled back and it gets closed. Let’s assume that somewhere in your application,
or in a third-party library, there is code such as the following:
functionsomeFunction(ManagerRegistry$registry){$em=$registry->getManager();$em->getConnection()->beginTransaction();try{// Do something.$em->getConnection()->commit();}catch(\Exception$ex){$em->getConnection()->rollback();$em->close();throw$ex;}}
If that code throws an exception and the EntityManager is closed. Any other
code which depends on the same instance of the EntityManager during this
request will fail.
On the other hand, if you instead inject the ManagerRegistry, the getManager()
method guarantees that you will always get a usable manager instance.
Loading history...
29
{
30
4
$this->entityManager = $entityManager;
31
4
$this->className = $className;
32
4
$this->name = Utils::getIDTypeName($className);
33
4
$this->description = 'Automatically generated type to be used as input where an object of type `' . Utils::getTypeName($className) . '` is needed';
34
35
4
parent::__construct();
36
4
}
37
38
/**
39
* Serializes an internal value to include in a response.
The expression $this->getRepository()->find($value); of type object|null adds the type object to the return on line 61 which is incompatible with the return type of the parent method GraphQL\Type\Definition\IDType::parseValue of type string.
Loading history...
62
}
63
64
/**
65
* Get the repository for our entity
66
* @return EntityRepository
67
*/
68
1
private function getRepository(): EntityRepository
The
EntityManager
might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during this request will fail.On the other hand, if you instead inject the
ManagerRegistry
, thegetManager()
method guarantees that you will always get a usable manager instance.