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,
],
],

Setting up a database adapter的更多相关文章

  1. ORA-16047: DGID mismatch between destination setting and target database

    做DG的时候 主库两个节点无法把日志传到备库上 SQL> select dest_name,status,type,database_mode,protection_mode,destinati ...

  2. [转]Setting the NLog database connection string in the ASP.NET Core appsettings.json

    本文转自:https://damienbod.com/2016/09/22/setting-the-nlog-database-connection-string-in-the-asp-net-cor ...

  3. Gem::LoadError: Specified 'sqlite3' for database adapter, but the gem is not loaded

    解决办法: 指定sqlite3的版本为1.3.13: gem 'sqlite3', '~> 1.3.13' 然后运行bundle update

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

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

  5. P6 EPPM Manual Installation Guide (Oracle Database)

    P6 EPPM Manual Installation Guide (Oracle Database) P6 EPPM Manual Installation Guide (Oracle Databa ...

  6. 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 ...

  7. DataBase: MySQL在.NET中的应用

    首先需要下载MySQL: 1. 官方下载 dev.mysql.com/downloads/mysql/ 2. 解压到你所想要安装的位置,在文件夹里创建my.ini文件 [mysql] # 设置mysq ...

  8. Pyhton开源框架(加强版)

    info:Djangourl:https://www.oschina.net/p/djangodetail: Django 是 Python 编程语言驱动的一个开源模型-视图-控制器(MVC)风格的 ...

  9. Unit Testing a zend-mvc application

    Unit Testing a zend-mvc application A solid unit test suite is essential for ongoing development in ...

随机推荐

  1. (七)学习MVC之CodeFirst迁移更新数据库

    1.首先在程序包管理控制台输入:enable-migrations -force ,然后回车: 问题1: The EntityFramework package is not installed on ...

  2. ubuntu常用快捷键

    1.直接退出窗口  Ctrl + q 2.窗口变大变小  Alt + Q 3.最小化窗口,显示桌面Ctrl + Alt + D 4.把当前窗口移到另一个工作区 Shift+ Ctrl + Alt +方 ...

  3. Linux批量修改指定目录下的文件或文件夹权限

    在Puppet下很头大,尤其是文件拷贝,使用file的mode会导致文件或文件夹都一个权限. 暂时使用命令代替: 最近忙着明年的N多计划,待有空后继续研究.

  4. 【原】Storm调度器

    Storm入门教程 1. Storm基础 Storm Storm主要特点 Storm基本概念 Storm调度器 Pluggable scheduler(可插拔调度器) Isolation schedu ...

  5. 【译】 AWK教程指南 10编写可与用户交互的AWK程序

    执行awk程序时,awk会自动从文件中读取数据来进行处理,直到文件结束.只要将awk读取数据的来源改成键盘输入,便可设计与awk 交互的程序.本节将提供一个该类程序的范例. 范例:本节将编写一个英语生 ...

  6. Spring配置文件的加载,及装载多个beans.xml文件

    applicationContext.xml 是spring的全局配置文件,用来控制srping的特性 1  手动加载自定义的beans.xml文件 @Test public void testAut ...

  7. linux内存负载分析

    衡量内存负载的一个很重要的指标就是页面置换的频率.当linux系统频繁的对页进行换进换出 的时候,说明物理内存不过,不得不进行频繁的置换页面. 使用vmstat(virtual memory stat ...

  8. 获取最外层View

    获取最外层View activity.getWindow().getDecorView()

  9. 转载 SharePoint Foundation和SharePoint Server的区别

    SharePoint Server 2010用来取代MOSS 2007,它有标准版和企业版两个版本,使用SQL Server数据库: 早期版本中的STS或WSS在2010中更名为SharePoint ...

  10. 射频识别技术漫谈(10)——识别号的格式变化【worldsing笔记】

    从事RDID行业的朋友经常会遇到这样的情况,同一张ID卡,在不同厂家生产的读卡器上读出的识别号完全不一样,有时甚至差之千里.ID卡的识别号一般是在出厂时被固化在卡片的ROM里,本身是不会改变的,问题出 ...