# Doctrine EntityManager

**URL:** <https://discourse.laminas.dev/t/doctrine-entitymanager/2984>\
**Category:** Mezzio\
**Tags:** doctrine\
**Created:** [July 30, 2022, 6:14pm UTC](https://discourse.laminas.dev/t/doctrine-entitymanager/2984 "2022-07-30T18:14:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jslmorrison](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/jslmorrison/32/1343_2.png) [@jslmorrison](https://discourse.laminas.dev/u/jslmorrison)\
**Post date:** [July 30, 2022, 6:14pm UTC](https://discourse.laminas.dev/t/doctrine-entitymanager/2984/1 "2022-07-30T18:14:14Z")

</div>

Hi,  
Whats the correct way to use doctrine in Mezzio? Specifically how to obtain instance of the Doctrine\ORM\EntityManager from the container within a factory to pass as constructor argument of related handler?

I’m currently getting ‘You have requested a non-existent service “Doctrine\ORM\EntityManagerInterface”’ and also unsure how and where to define the database parameters in my module.

I have the following factory:

```auto
final class UsersHandlerFactory
{
    public function __invoke(ContainerInterface $container): RequestHandlerInterface
    {
        $entityManager = $container->get(EntityManagerInterface::class);

        return new UsersHandler($entityManager);
    }
}

```

and in related handler:

```auto
public function __construct(private EntityManagerInterface $entityManager)
{
}

```

and config:

```auto
public function getDependencies(): array
    {
        return [
            'invokables' => [],
            'factories' => [
                Handler\UsersHandler::class => Handler\UsersHandlerFactory::class,
            ],
        ];
    }

```

Thanks

---

<div class="post-metadata">

**Author:** ![ezkimo](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/ezkimo/32/781_2.png) [@ezkimo](https://discourse.laminas.dev/u/ezkimo)\
**Post date:** [July 31, 2022, 8:44am UTC](https://discourse.laminas.dev/t/doctrine-entitymanager/2984/2 "2022-07-31T08:44:06Z")

</div>

The correct way retrieving an `EntityManager` instance from the service container is …

```auto
<?php
declare(strict_types=1);
namespace Marcel\Factory;

use Doctrine\ORM\EntityManager;
...
public function __invoke(ContainerInterface $container): RequestHandlerInterface
{
    $entityManager = $container->get(EntityManager::class);
    return new UsersHandler($entityManager);
}
...

```

Instead of getting `EntityManagerInterface::class` just use `EntityManager::class`. That should fix your issue. The type hints are okay, because the entity manager implements the interface. I personally use `Doctrine\Persistence\ObjectManager` as type hint, because it 's the most basic interface. Using the `EntityManagerInterface` is fine, too.

---

<div class="post-metadata">

**Author:** ![jslmorrison](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/jslmorrison/32/1343_2.png) [@jslmorrison](https://discourse.laminas.dev/u/jslmorrison)\
**Post date:** [July 31, 2022, 10:24am UTC](https://discourse.laminas.dev/t/doctrine-entitymanager/2984/3 "2022-07-31T10:24:21Z")

</div>

Hi @ezkimo,  
Thanks for the reply but unfortunately that did not work either. Still requesting a non existent service.  
I’ll need to investigate further to establish where the services get defined and how to inspect whats available in the container.

---

<div class="post-metadata">

**Author:** ![ezkimo](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/ezkimo/32/781_2.png) [@ezkimo](https://discourse.laminas.dev/u/ezkimo)\
**Post date:** [July 31, 2022, 10:36am UTC](https://discourse.laminas.dev/t/doctrine-entitymanager/2984/4 "2022-07-31T10:36:54Z")

</div>

Hey,

do you use the vendor package `roave/psr-container-doctrine`? If not, what do you use instead for doctrine implementation in mezzio?

If you 're using `roave/psr-container-doctrine` you can define a factory in your application config.

```auto
return [
    'dependencies' => [
        'factories' => [
            'doctrine.entity_manager.orm_default' => 
                \Roave\PsrContainerDoctrine\EntityManagerFactory::class,
        ],
    ],
];

```

Taken from: [GitHub - Roave/psr-container-doctrine: Doctrine Factories for PSR-11 Containers](https://github.com/Roave/psr-container-doctrine)

You 're able to call the entity manager instance by the name its registered in the config.

```auto
$container->get('doctrine.entity_manager.orm_default');

```

---

<div class="post-metadata">

**Author:** ![jslmorrison](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/jslmorrison/32/1343_2.png) [@jslmorrison](https://discourse.laminas.dev/u/jslmorrison)\
**Post date:** [August 2, 2022, 9:31pm UTC](https://discourse.laminas.dev/t/doctrine-entitymanager/2984/5 "2022-08-02T21:31:57Z")

</div>

Hi - thanks for the reply. Yes was using that but had it incorrectly configured. I’ve moved the config into:  
`config/autoload/development.local.php`  
which hopefully is correct, but then receive another error:  
`Class "Doctrine\Common\Cache\ArrayCache" not found`

So need to familiarise myself with the config side of things more 🙂

---

<div class="post-metadata">

**Author:** ![PowerKiKi](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/powerkiki/32/48_2.png) [@PowerKiKi](https://discourse.laminas.dev/u/PowerKiKi)\
**Post date:** [August 2, 2022, 10:09pm UTC](https://discourse.laminas.dev/t/doctrine-entitymanager/2984/6 "2022-08-02T22:09:32Z")

</div>

If you are willing to dig into some code, you can have a look at a real-life example.

We configure `roave/psr-container-doctrine` in [config/autoload/doctrine.global.php](https://github.com/Ecodev/my-ichtus/blob/master/config/autoload/doctrine.global.php). It could be a different file name, but it make sense to me to split doctrine stuff from other things. (the actual login/password will be in a non-versioned `config/autoload/local.php`).

Then we configure the container with an alias first in [my-ichtus/dependencies.global.php at a4d77f0b05ec7c5d065787c11c03982508701c22 · Ecodev/my-ichtus · GitHub](https://github.com/Ecodev/my-ichtus/blob/a4d77f0b05ec7c5d065787c11c03982508701c22/config/autoload/dependencies.global.php#L13). And most importantly we configure the EntityManager factory in [my-ichtus/dependencies.global.php at a4d77f0b05ec7c5d065787c11c03982508701c22 · Ecodev/my-ichtus · GitHub](https://github.com/Ecodev/my-ichtus/blob/a4d77f0b05ec7c5d065787c11c03982508701c22/config/autoload/dependencies.global.php#L23).

In our case our custom [EntityManagerFactory](https://github.com/Ecodev/my-ichtus/blob/master/server/Application/ORM/EntityManagerFactory.php) is pretty straighforward and is only needed for our special use of filters. Most likely you can take a shortcut and configure the container directly with `\Roave\PsrContainerDoctrine\EntityManagerFactory` instead.

Finally all of this can be used like this: [my-ichtus/ImageFactory.php at a4d77f0b05ec7c5d065787c11c03982508701c22 · Ecodev/my-ichtus · GitHub](https://github.com/Ecodev/my-ichtus/blob/a4d77f0b05ec7c5d065787c11c03982508701c22/server/Application/Handler/ImageFactory.php#L17:)

```php
$entityManager = $container->get(EntityManager::class);

```
