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. android Bitmap getByteCount和getRowBytes

    今天做图像缓存需要计算Bitmap的所占的内存空间,于是研究了下Bitmap关于内存占用的API 1.getRowBytes:Since API Level 1,用于计算位图每一行所占用的内存字节数. ...

  2. [转] windows7 IIS管理器 在计算机“.”上没有找到WAS服务

    原文地址:windows7 IIS管理器 在计算机"."上没有找到WAS服务作者:云中的风 OS:windows7旗舰版 产生问题原因:运行金蝶K3-HR时客户端提示中间层服务器不 ...

  3. SDUT 3258 Square Number 简单数学

    和上一题一样,把平方因子除去,然后对应的数就变成固定的 #include <cstdio> #include <iostream> #include <algorithm ...

  4. 黑盒测试用例设计方法&理论结合实际 -> 等价类划分

    一. 概念 等价类划分法是把程序的输入域划分成若干部分(子集),然后从每个部分中选取少数代表性数据作为测试用例.每一类的代表性数据在测试中的作用等价于这一类中的其他值. 二. 等价类划分的应用 等价类 ...

  5. Java面试题:n=2\n1*2*5*6\n--3*4\n\nn=3\n1*2*3*10*11*12\n--4*5*8*9\n----6*7\n如何实现如上结构的数据

    今天学长在面试的时候遇到了一道题,然后让大家做一做. 在不看下面的答案之前,悠闲的朋友们一起来抖动一下大脑吧! 以下是我的想法: import java.util.Scanner;public cla ...

  6. python 网络编程(一)---基础

    1.理解TCP 1.1 寻址 首先任何基于TCP/IP的网络,每台机器都有唯一的IP地址,只需要知道接受者机器的IP地址,信息就可以传送过去. 其次,TCP通过使用端口号来知道机器究竟是哪个程序应该接 ...

  7. Android选项卡TabHost方式实现

    1.布局XML: <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android= ...

  8. CentOS 6.5 安装配置VSFTP

    1.下载安装VSFTP 首先查看当前系统是否已经安装VSFTP,若未安装则使用yum安装. chkconfig --list | grep vsftpd #查看是否安装 yum install vsf ...

  9. linux oracle10g安装

    同样适合11g哦,亲; 一.安装系统 首先安装Linux系统,根据Oracle官方文档的建议,在机器内存小于1G的情况下,swap分区大小应该设置为内存的2倍大,若内存大于2G则swap分区设置为与内 ...

  10. POJ2723-Get Luffy Out(2-SAT)

    题意:有m扇门,每个门上有两把锁,打开任意一个锁都可以打开这扇门.门要按顺序一个一个打开. 现在有n对不同的钥匙,每对钥匙只能用其中一个,问最多能打开多少门. 题解:对钥匙建图,门是限制条件来建边.每 ...