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.phpormodules.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 either
config/application.config.phporconfig/modules.config.phpto 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的更多相关文章
- ORA-16047: DGID mismatch between destination setting and target database
做DG的时候 主库两个节点无法把日志传到备库上 SQL> select dest_name,status,type,database_mode,protection_mode,destinati ...
- [转]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 ...
- Gem::LoadError: Specified 'sqlite3' for database adapter, but the gem is not loaded
解决办法: 指定sqlite3的版本为1.3.13: gem 'sqlite3', '~> 1.3.13' 然后运行bundle update
- BizTalk 2013R2 WCF-LOB Oracle Adapter安装配置/问题&解决方法
BizTalk 2013R2 WCF-LOB Oracle Adapter安装配置/问题&解决方法 安装Oracle Adapter 安装Oracle客户端 BizTalk 2013R2 安装 ...
- P6 EPPM Manual Installation Guide (Oracle Database)
P6 EPPM Manual Installation Guide (Oracle Database) P6 EPPM Manual Installation Guide (Oracle Databa ...
- 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 ...
- DataBase: MySQL在.NET中的应用
首先需要下载MySQL: 1. 官方下载 dev.mysql.com/downloads/mysql/ 2. 解压到你所想要安装的位置,在文件夹里创建my.ini文件 [mysql] # 设置mysq ...
- Pyhton开源框架(加强版)
info:Djangourl:https://www.oschina.net/p/djangodetail: Django 是 Python 编程语言驱动的一个开源模型-视图-控制器(MVC)风格的 ...
- Unit Testing a zend-mvc application
Unit Testing a zend-mvc application A solid unit test suite is essential for ongoing development in ...
随机推荐
- 【Struts】strust.xml中<result type="">所有类型详解
在默认时,<result>标签的type属性值是“dispatcher”(实际上就是转发,forward).开发人员可以根据自己的需要指定不同的类型,如redirect.stream等.如 ...
- Spring AOP--返回通知,异常通知和环绕通知
在上篇文章中学习了Spring AOP,并学习了前置通知和后置通知.地址为:http://www.cnblogs.com/dreamfree/p/4095858.html 在本文中,将继续上篇的学习, ...
- AM335x(TQ335x)学习笔记——挂载Ramdisk
上篇文章中我们已经能够通过u-boot启动内核了,但是没有能够启动成功,从内核的log中可以看出,内核启动失败的原因是没有挂载到root文件系统,本文将使用busybox制作根文件系统并打包成ramd ...
- About Wisdom
All human wisdom is summed up in two words --- wait and hope.人类所有的智慧可以归结为两个词---等待和希望. —— Alexandre D ...
- 什么是MBeanServer
什么是MBeanServer MBeanServer是一个包含所有注册MBean的仓库.它是JMX代理层的核心.JMX1.0规范提供一个接口叫 javax.management.MBeanServer ...
- Codeforces 633 C Spy Syndrome 2 字典树
题意:还是比较好理解 分析:把每个单词反转,建字典树,然后暴力匹配加密串 注:然后我就是特别不理解,上面那种能过,而且时间很短,但是我想反之亦然啊 我一开始写的是,把加密串进行反转,然后单词正着建字典 ...
- 转:eclipse导入工程中文乱码问题
eclipse之所以会出现乱码问题是因为eclipse编辑器选择的编码规则是可变的.一般默认都是UTF-8或者GBK,当从外部导入的一个工程时,如果该工程的编码方式与eclipse中设置的编码方式不同 ...
- 我用的Linux命令
从今天起,会在这里记一些我使用过的linux命令 =======I'm 分割线======= 11.查看某个端口号被哪个应用占用 netstat -apn|grep 端口号,lsof -i:端口号,n ...
- python 定义函数
在Python中,定义一个函数要使用def语句,依次写出函数名.括号.括号中的参数和冒号:,然后,在缩进块中编写函数体,函数的返回值用return语句返回. 我们以自定义一个求绝对值的my_abs函数 ...
- [iOS基础控件 - 5.1] UIScrollView
A.需要掌握 UIScrollView 是一个能够滚动的视图控件,可以用来展示大量内容,如手机的“设置” 1.常见属性 2.常用代理方法 3.缩放 4.UIScrollView和UIPageContr ...