EntityManager and cache (slim 4)

Good morning,

firstly, I want to inform you that English is not my first language, and I apologize for any imperfections in my English. I use EntityManager for Doctrine ORM. I read that it’s good to insert cache, so I added it (for development and production). Here is a portion of the program in container.php:

    // Construction de l'instance EntityManager
    EntityManager::class => function (ContainerInterface $container) : EntityManager {

        $settings = (array)$container->get('settings')['doctrine'];
        
        $cache = $settings['isDevMode'] ?
            new ArrayAdapter() : 
            new FilesystemAdapter(directory: $settings['cache_dir']);
        
        $paths = [$settings['pathToModels']]; //chemin de l'entité
        $isDevMode = $settings['isDevMode']; // est-ce en développement?

        // paramètres de connexion
        $dbParams = $settings['connectionMysql']; //connexion de la base de données
         
        // construction er retour de l'instance AppFactory
        $config = ORMSetup::createAttributeMetadataConfiguration($paths, $isDevMode);
        $connection = DriverManager::getConnection($dbParams, $config);
        return new EntityManager($connection, $config);   
    },

Is the syntax of the cache good?

Thanks for your response.

I think this DI container definition does too much. It creates a database connection and an EntityManager. I would recommend separating these different tasks, i.e. I would move the logic for the Connection instance into its own DI container definition.

Good morning,

I find a solution with the help of your book (Doctrine DBAL) and Github Copilot. I didn’t know how to pass informations from "connection "definition to the entityManager definition (Sorry, I’m novice)
It works. But, I don’t have complete trust in the Github Copilot.
This is the code:

// 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);  

Is it correct?

Have a good day and thanks for yours answers.

Yes, this looks much better. :+1: