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 ...
随机推荐
- 【转】JAVA中的反射机制
反射,当时经常听他们说,自己也看过一些资料,也可能在设计模式中使用过,但是感觉对它没有一个较深入的了解,这次重新学习了一下,感觉还行吧! 一,先看一下反射的概念: 主要是指程序可以访问,检测和修改它本 ...
- 【转】tomcat下jndi的三种配置方式
jndi(Java Naming and Directory Interface,Java命名和目录接口)是一组在Java应用中访问命名和目录服务的API.命名服务将名称和对象联系起来,使得我们可以用 ...
- MVC 5 的 EF6 Code First 入门
英文渣水平,大伙凑合着看吧…… 这是微软官方SignalR 2.0教程Getting Started with Entity Framework 6 Code First using MVC 5 系列 ...
- EF Code First学习笔记:数据库创建
控制数据库的位置 默认情况下,数据库是创建在localhost\SQLEXPRESS服务器上,并且默认的数据库名为命名空间+context类名,例如我们前面的BreakAway.BreakAwayCo ...
- Ramdisk文件系统的制作与调试运行
开发环境:Fedora 9交叉编译工具链:arm-linux-gcc 4.3.2 with EABI嵌入式Linux内核版本:2.6.29.4-FriendlyARM.昨天写贴子的时候具体记不清了,今 ...
- Win32汇编环境配置
放假了,发现自己知识面窄,趁有时间就打算折腾下Win32汇编.其实在学校也上过汇编课,是基于dos的.那时老师不务正业,老跟我们讲政治经济文化,唯独不怎么讲课;再加上自己的问题,导致了dos汇编学得好 ...
- vs2015无法解析外部符号__imp__fprintf
使用vs2015编译ffmpeg的一个小项时,出现了__imp__fprintf和__imp____iob_func 的错误,google了一下,有的人 建议下载SDL源码重新编译一下,当然这个方案非 ...
- LeetCode题解——ZigZag Conversion
题目: 把一个字符串按照Z型排列后打印出来,例如 "PAYPALISHIRING" 重新排列后为3行,即 P A H N A P L S I I G Y I R 那么输出为&quo ...
- 那传说中的P、NP以及NPC问题
那传说中的P.NP以及NPC问题 (这里只是自己的一些总结) 在讲这几个问题之前,有几个东西是必须要说的,包括时间复杂度.空间复杂度.图灵机什么的.那么我们就慢慢来一一说来. 图灵机:图 ...
- 教程-DelphiXE7如何调用Java Class,JAR等文件?
源文地址:http://jingyan.baidu.com/article/e4d08ffdb61b040fd3f60d44.html 第一步,我们先在互联网上把java2pas这个工具下载下来. 下 ...