the DI container definition Entitymanager does too much. So It’s preferable to create an another one for the connexion (EntityManager and cache (slim 4) - #2 by odan)
// connexion à la base de données
Connection::class => function (ContainerInterface $container) {
$settings = (array)$container->get('settings')['doctrine'];
// paramètres de connexion
$dbParams = $settings['connectionMysql'];
$config = $container->get('doctrineConfig');
return DriverManager::getConnection($dbParams, $config);
},
//Configuration
'doctrineConfig' => function (ContainerInterface $container) {
$settings = (array)$container->get('settings')['doctrine'];
return ORMSetup::createAttributeMetadataConfiguration(
[$settings['pathToModels']], //chemin des l'entités
$settings['isDevMode'] //est-on en développement?
);
},
//EntityManager
EntityManager::class => function (ContainerInterface $container) : EntityManager {
$settings = (array)$container->get('settings')['doctrine'];
$cache = $settings['isDevMode'] ?
new ArrayAdapter() :
new FilesystemAdapter(directory: $settings['cache_dir']);
$connection = $container->get(Connection::class);
$config = $container->get('doctrineConfig');
return new EntityManager($connection, $config);