当前位置:Gxlcms > 数据库问题 > Setting up a database adapter

Setting up a database adapter

时间:2021-07-01 10:21:17 帮助过:7人阅读

Configuring the default adapter

Within your service factories, you may retrieve the default adapter from your application container using the class name Zend\Db\Adapter\AdapterInterface:

use Zend\Db\Adapter\AdapterInterface;

function ($container) {
    return new SomeServiceObject($container->get(AdapterInterface::class));
}

When installed and configured, the factory associated with AdapterInterfacewill look for a top-level db key in the configuration, and use it to create an adapter. As an example, the following would connect to a MySQL database using PDO, and the supplied PDO DSN:

return [
    ‘db‘ => [
        ‘driver‘ => ‘Pdo‘,
        ‘dsn‘    => ‘mysql:dbname=zf2tutorial;host=localhost‘,
    ],
];

More information on adapter configuration can be found in the docs forZend\Db\Adapter.

Configuring named adapters

Sometimes you may need multiple adapters. As an example, if you work with a cluster of databases, one may allow write operations, while another may be read-only.

zend-db provides an abstract factory,Zend\Db\Adapter\AdapterAbstractServiceFactory, for this purpose. To use it, you will need to create named configuration keys under db.adapters, each with configuration for an adapter:

return [
    ‘db‘ => [
        ‘adapters‘ => [
            ‘Application\Db\WriteAdapter‘ => [
                ‘driver‘ => ‘Pdo‘,
                ‘dsn‘    => ‘mysql:dbname=application;host=canonical.example.com‘,
            ],
            ‘Application\Db\ReadOnlyAdapter‘ => [
                ‘driver‘ => ‘Pdo‘,
                ‘dsn‘    => ‘mysql:dbname=application;host=replica.example.com‘,
            ],
        ],
    ],
];

You retrieve the database adapters using the keys you define, so ensure they are unique to your application, and descriptive of their purpose!

Retrieving named adapters

Retrieve named adapters in your service factories just as you would another service:

function ($container) {
    return new SomeServiceObject($container->get(‘Application\Db\ReadOnlyAdapter));
}

Using the AdapterAbstractServiceFactory as a factory

Depending on what application container you use, abstract factories may not be available. Alternately, you may want to reduce lookup time when retrieving an adapter from the container (abstract factories are consulted last!). zend-servicemanager abstract factories work as factories in their own right, and are passed the service name as an argument, allowing them to vary their return value based on requested service name. As such, you can add the following service configuration as well:

use Zend\Db\Adapter\AdapterAbstractServiceFactory;

// If using zend-mvc:
‘service_manager‘ => [
    ‘factories‘ => [
        ‘Application\Db\WriteAdapter‘ => AdapterAbstractServiceFactory::class,
    ],
],

// If using Expressive
‘dependencies‘ => [
    ‘factories‘ => [
        ‘Application\Db\WriteAdapter‘ => AdapterAbstractServiceFactory::class,
    ],
],

Setting up a database adapter

标签:

人气教程排行