1

我尝试使用 2 个 PDO 数据库连接设置 SLIM 4。
我无法从存储库访问 db 或 db2。
我想我做错了什么

我跟着苗条的4骨架。
我在应用程序中创建了一个 db.php 文件,用于在容器中添加 db 的定义
我在 index.php 中包含 db.php 我试图从存储库访问 db 或 db2,但它不起作用

这是我的文件:

应用程序/db.php

    declare(strict_types=1);

    use PDO;
    use DI\ContainerBuilder;

    return function (ContainerBuilder $containerBuilder) {
        // Global Settings Object
        $containerBuilder->addDefinitions([
            'db' => function (ContainerInterface $c) {
                $settings = $c->get('settings');
                $dbSettings = $settings['db'];
                return new PDO('mysql:host=' . $dbSettings['host'] . ';dbname=' . $dbSettings['db'], $dbSettings['username'], $dbSettings['password'], $dbSettings['options']);
            },
            'db2' => function (ContainerInterface $c) {
                $settings = $c->get('settings');
                $dbSettings = $settings['db2'];
                return new PDO('mysql:host=' . $dbSettings['host'] . ';dbname=' . $dbSettings['db'], $dbSettings['username'], $dbSettings['password'], $dbSettings['options']);
            }
        ]);
    };

公共/index.php

declare(strict_types=1);

use App\Application\Handlers\HttpErrorHandler;
use App\Application\Handlers\ShutdownHandler;
use App\Application\ResponseEmitter\ResponseEmitter;
use DI\ContainerBuilder;
use Slim\Factory\AppFactory;
use Slim\Factory\ServerRequestCreatorFactory;

require __DIR__ . '/../vendor/autoload.php';

// Instantiate PHP-DI ContainerBuilder
$containerBuilder = new ContainerBuilder();

if (false) { // Should be set to true in production
    $containerBuilder->enableCompilation(__DIR__ . '/../var/cache');
}

// Set up settings
$settings = require __DIR__ . '/../app/settings.php';
$settings($containerBuilder);

// Set up dependencies
$dependencies = require __DIR__ . '/../app/dependencies.php';
$dependencies($containerBuilder);

// Set up db, added by PF
$db = require __DIR__ . '/../app/db.php';
$db($containerBuilder);

// Set up repositories
$repositories = require __DIR__ . '/../app/repositories.php';
$repositories($containerBuilder);

// Build PHP-DI Container instance
$container = $containerBuilder->build();

// Instantiate the app
AppFactory::setContainer($container);
$app = AppFactory::create();
$callableResolver = $app->getCallableResolver();

// Register middleware
$middleware = require __DIR__ . '/../app/middleware.php';
$middleware($app);

// Register routes
$routes = require __DIR__ . '/../app/routes.php';
$routes($app);

/** @var bool $displayErrorDetails */
$displayErrorDetails = $container->get('settings')['displayErrorDetails'];

// Create Request object from globals
$serverRequestCreator = ServerRequestCreatorFactory::create();
$request = $serverRequestCreator->createServerRequestFromGlobals();

// Create Error Handler
$responseFactory = $app->getResponseFactory();
$errorHandler = new HttpErrorHandler($callableResolver, $responseFactory);

// Create Shutdown Handler
$shutdownHandler = new ShutdownHandler($request, $errorHandler, $displayErrorDetails);
register_shutdown_function($shutdownHandler);

// Add Routing Middleware
$app->addRoutingMiddleware();

// Add Error Middleware
$errorMiddleware = $app->addErrorMiddleware($displayErrorDetails, false, false);
$errorMiddleware->setDefaultErrorHandler($errorHandler);

// Run App & Emit Response
$response = $app->handle($request);
$responseEmitter = new ResponseEmitter();
$responseEmitter->emit($response);

src/域/ObContact/ObContactRepository.php

declare(strict_types=1);

namespace App\Domain\ObContact;

use PDO;

class ObContactRepository
{
    /**
     * @var PDO The database db
     */
    private $db;
    private $db2;

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

    public function findByEmail(string $email)
    {
        if (empty($email))
            return false;

        return $this->db
            ->query('SELECT * FROM ob_contact WHERE email=:email')
            ->execute([':email' => $email])
            ->fetch();
    }
}
4

1 回答 1

-1

你安装了PDO吗?

$ composer require faapz/pdo 

来源: https ://github.com/FaaPz/PDO

于 2020-01-05T18:18:01.793 回答