You have injected the EntityManager via parameter $em. 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...
25
{
26
$this->em = $em;
27
}
28
29
public function adaptForm(AdaptFormEvent $event)
30
{
31
if ($event->getPage() instanceof HasNodeInterface && !$event->getPage()->isStructureNode()) {
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method findOrCreateFor() does only exist in the following implementations of said interface: Kunstmaan\SeoBundle\Repository\SeoRepository.
Let’s take a look at an example:
interfaceUser{/** @return string */publicfunctiongetPassword();}classMyUserimplementsUser{publicfunctiongetPassword(){// return something}publicfunctiongetDisplayName(){// return some name.}}classAuthSystem{publicfunctionauthenticate(User$user){$this->logger->info(sprintf('Authenticating %s.',$user->getDisplayName()));// do something.}}
In the above example, the authenticate() method works fine as long as you just pass
instances of MyUser. However, if you now also want to pass a different implementation
of User which does not have a getDisplayName() method, the code will break.
classAuthSystem{publicfunctionauthenticate(User$user){if($userinstanceofMyUser){$this->logger->info(/** ... */);}// or alternativelyif(!$userinstanceofMyUser){thrownew\LogicException('$user must be an instance of MyUser, '.'other instances are not supported.');}}}
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types
inside the if block in such a case.
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.