Setting up a database adapter

Setting up a database adapter

zend-db provides a general purpose database abstraction layer. At its heart is the Adapter, which abstracts common database operations across the variety of drivers we support.

In this guide, we will document how to configure both a single, default adapter as well as multiple adapters (which may be useful in architectures that have a cluster of read-only replicated servers and a single writable server of record).

Installing zend-db

First, install zend-db using Composer:

$ composer require zendframework/zend-db

If you are using zend-component-installer (installed by default with the skeleton application, and optionally for Expressive applications), you will be prompted to install the package configuration.

  • For zend-mvc applications, choose either application.config.php ormodules.config.php.
  • For Expressive applications, choose config/config.php.

If you are not using the installer, you will need to manually configure add the component to your application.

  • For zend-mvc applications, update your list of modules in eitherconfig/application.config.php or config/modules.config.php to add an entry for ‘Zend\Db‘ at the top of the list.
  • For Expressive applications, create a new file,config/autoload/zend-db.global.php, with the following contents:
<?php
use Zend\Db\ConfigProvider;

return (new ConfigProvider())();

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,
    ],
],
时间: 2024-09-29 00:54:06

Setting up a database adapter的相关文章

Specified &#39;postgresql&#39; for database adapter, but the gem is not loaded.

https://stackoverflow.com/questions/24755673/gemloaderror-specified-postgresql-for-database-adapter-but-the-gem-is-not Specified 'postgresql' for database adapter, but the gem is not loaded. 原文地址:https://www.cnblogs.com/ihibin/p/8215680.html

Gem::LoadError: Specified &#39;sqlite3&#39; for database adapter, but the gem is not loaded

解决办法: 指定sqlite3的版本为1.3.13: gem 'sqlite3', '~> 1.3.13' 然后运行bundle update Gem::LoadError: Specified 'sqlite3' for database adapter, but the gem is not loaded 原文地址:https://www.cnblogs.com/wtyqer/p/10792159.html

P6 EPPM Manual Installation Guide (Oracle Database)

Contents Oracle Database Manual Configuration Overview ,,★★5 Oracle Database Installation ,,★★6 Creating the Database Structure for Oracle and Loading Application Data ,,★★7 Creating the P6 EPPM Database Structure for Oracle ,,★★7 Copying the Script

P6 Professional Installation and Configuration Guide (Microsoft SQL Server Database) 16 R1

P6 Professional Installation and Configuration Guide (Microsoft SQL Server Database) 16 R1       May 2016 Contents About This Guide...................................................................................... 11 Shared Topics in This Guide .

Setting Up and Configuring Backup and Recovery1

3.Setting Up and Configuring Backup and Recovery 这个单元讲述如何启动.与rman client如何互动,准备rman环境,实现备份和恢复策略 注意:尽管闪回数据库和安全还原点不是真的数据库备份,但是它们是数据保护策略一个重要部分.这些特性需要一些初始化设置,这些设置依赖于在备份策略中你怎么混合它们.Chapter 5-Data Protection with Restore Points andFlashback Database 提供了关于怎么

BizTalk 2013R2 WCF-LOB Oracle Adapter安装配置/问题&解决方法

BizTalk 2013R2 WCF-LOB Oracle Adapter安装配置/问题&解决方法 安装Oracle Adapter 安装Oracle客户端 BizTalk 2013R2 安装包自带的WCF LOB Adapter Pack for Oracle 需要Oracle客户端组件 ODP.NET或ODAC组件.这里很容易出现问题,版本的不兼容,因为Oracle Adapter默认需要安装 11.1.0.7这个版本 The Oracle Database adapter uses the

Unit Testing a zend-mvc application

Unit Testing a zend-mvc application A solid unit test suite is essential for ongoing development in large projects, especially those with many people involved. Going back and manually testing every individual component of an application after every c

第五章:Reminders实验:第一部分[Learn Android Studio 汉化教程]

Learn Android Studio 汉化教程 By now you are familiar with the basics of creating a new project, programming, and refactoring.It is time to create an Android application, otherwise known as an app. This chapter introduces the first of four lab projects.

destoon 给超级管理员系统权限(管理员管理,日志管理等)

destoon 后台某些系统权限除了网站创始人之外其他超管事没有权限的,现需要给其他超级管理员添加普通管理员的权限. 1.首先  admin/global.func.php  admin_check函数 ,去掉admin  这样超管才能访问 ?file=admin } else if($_admin == 1) {?     //if(in_array($file, array('admin', 'setting', 'module', 'area', 'database', 'template