How to inject multiple PDO instances in Slim 4

﹥>﹥吖頭↗ 提交于 2021-01-29 14:10:39

问题


I've just started using Slim 4 (also brand new to Slim as a whole) and after reading up and following some articles I've managed to get a skeleton app setup with a PDO connection to the DB.

I'm now looking to inject a second PDO instance so that a second database can be used based on the request, though I'm struggling to get my head around how to do this.

My current setup is:

settings.php

$settings['db'] = [
    'driver' => 'mysql',
    'host' => 'database',
    'username' => 'root',
    'database' => 'demo',
    'password' => 'password',
    'flags' => [
        // Turn off persistent connections
        PDO::ATTR_PERSISTENT => false,
        // Enable exceptions
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        // Emulate prepared statements
        PDO::ATTR_EMULATE_PREPARES => true,
        // Set default fetch mode to array
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    ],
];

container.php

return [
    Configuration::class => function () {
        return new Configuration(require __DIR__ . '/settings.php');
    },

    App::class => function (ContainerInterface $container) {
        AppFactory::setContainer($container);
        $app = AppFactory::create();

        return $app;
    },

    PDO::class => function (ContainerInterface $container) {
        $config = $container->get(Configuration::class);

        $host = $config->getString('db.host');
        $dbname =  $config->getString('db.database');
        $username = $config->getString('db.username');
        $password = $config->getString('db.password');
        $dsn = "mysql:host=$host;dbname=$dbname;";

        return new PDO($dsn, $username, $password);
    },

];

Example usage in repository

class UserReaderRepository
{
    /**
     * @var PDO The database connection
     */
    private $connection;

    /**
     * Constructor.
     *
     * @param PDO $connection The database connection
     */
    public function __construct(PDO $connection)
    {
        $this->connection = $connection;
    }

    /**
     * Get user by the given user id.
     *
     * @throws DomainException
     *
     * @return array The user data
     */
    public function getUsers(): array
    {
        $sql = "SELECT user_id, title, firstname, surname, email_address FROM user us;";
        $statement = $this->connection->prepare($sql);
        $statement->execute();

From what I can tell, the PDO class itself is instantiated within container.php - how do i modify this as to instantiate 2 separate PDO classes - and how do i use them within repositories later on?

For clarity, I've tried looking at How to set up and inject multiple PDO database connections in slim 4? but I don't quite understand where to define the PDO classes. I've also looked here which didn't seem to help - I don't have a dependencies.php, the closest I have is middleware.php but when I try this I get the error Uncaught Error: Cannot use object of type DI\Container as array in /var/www/config/middleware.php:

return function (App $app) {
    // Parse json, form data and xml
    $app->addBodyParsingMiddleware();

    // Add global middleware to app
    $app->addRoutingMiddleware();

    $container = $app->getContainer();

    // PDO database library
    $container['db'] = function ($c) {
        $settings = $c->get('settings')['db'];
        $pdo = new PDO("mysql:host=" . $settings['host'] . ";dbname=" . $settings['dbname'],
            $settings['user'], $settings['pass']);
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
        return $pdo;
    };

    // Error handler
    $settings = $container->get(Configuration::class)->getArray('error_handler_middleware');
    $display_error_details = (bool)$settings['display_error_details'];
    $log_errors = (bool)$settings['log_errors'];
    $log_error_details = (bool)$settings['log_error_details'];

    $app->addErrorMiddleware($display_error_details, $log_errors, $log_error_details);
};

回答1:


After some deliberation I think I've worked it out.

I used the container to create the PDO instances

$container = $app->getContainer();

    $container->set('db', function(ContainerInterface $c) {
        $config = $c->get(Configuration::class);

        $host = $config->getString('db.host');
        $dbname =  $config->getString('db.database');
        $username = $config->getString('db.username');
        $password = $config->getString('db.password');
        $dsn = "mysql:host=$host;dbname=$dbname;";

        return new PDO($dsn, $username, $password);
    });

    $container->set('db_readonly', function(ContainerInterface $c) {
        $config = $c->get(Configuration::class);

        $host = $config->getString('db_readonly.host');
        $dbname =  $config->getString('db_readonly.database');
        $username = $config->getString('db_readonly.username');
        $password = $config->getString('db_readonly.password');
        $dsn = "mysql:host=$host;dbname=$dbname;";

        return new PDO($dsn, $username, $password);
    });

Then changed the repository to use the App within the constructor, then using the container to get the PDO instances

/**
     * @var PDO The database
     */
    private $db;

    /**
     * @var PDO The readonly database
     */
    private $readonly_db;

    /**
     * Constructor.
     *
     * @param App $app The database db
     */
    public function __construct(App $app)
    {
        $container = $app->getContainer();
        $this->db = $container->get('db');
        $this->readonly_db = $container->get('db_readonly');
    }

Leaving this here in case anyone else has issues, although if there's a better way to do it, or my current method can be improved upon, I'd appreciate the feedback



来源:https://stackoverflow.com/questions/59906475/how-to-inject-multiple-pdo-instances-in-slim-4

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!