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 either
config/application.config.php
orconfig/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 AdapterInterface
will 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 ...
随机推荐
- tomcat server.xml配置详解
由于 Tomcat 基于 Java,实际上在各种 Linux 发行版里的配置方法都大同小异,只是我看见在 Arch Linux 环境里搭建 Tomcat 的文章比较少,所以在 Arch Linux 实 ...
- 使用SQL Server 2005作业设置定时任务
公司有一个老项目由于直接把终端拍摄的图片以二进制的形式保存到数据库中,数据库比较大所以需要经常删除这些冗余数据,手动删除费时费力,项目组长让我把这些操作变成自动的,每天执行一次,只保留最近两个月的图片 ...
- oracle 问题若干 提醒注意
1.Powerdesigner 里生成sql,在oracle中运行时报错:ORA-00907: 缺失右括号 解决:这样的问题很多时候是因为用了不正确的数据类型造成的.比如写作nvarchar(n),但 ...
- opencv开发的程序分发给客户时所需要的dll文件
这里主要讲在其他裸机,没有搭建开发环境机器上运行自己开发的程序. 为了测试,我专门用visualbox搭建了一个虚拟机(主机和虚拟机都是win7系统) 在发给别人程序运行出现错误:msvcp100d. ...
- 基于寄存器的VM
jvm是基于栈的,基于栈的原因是:实现简单,考虑的就是两个地方,局部变量和操作数栈 http://ifeve.com/javacode2bytecode/这几篇文章相当不错. http://redna ...
- Unity3D入门之JavaScript动态创建对象
接着上一篇Unity3D入门文章,这里继续使用JavaScript脚本语言. 调试:Unity集成了MonoDevelop编辑器,在代码某行的左侧点击,即可下一个断点.然后先关闭Unity编辑器,在M ...
- AVLTree的节点删除
当年实现自己的共享内存模板的时候,map和set的没有实现,本来考虑用一个AVLTree作为底层实现的,为啥,因为我当时的数据结构知识里面我和RBTree不熟,只搞过AVLTree,但当时我一直没有看 ...
- openstack python sdk list tenants get token get servers
1,openstack python sdk 获取token 获取租户tenants projects #!/bin/bash export OS_PROJECT_DOMAIN_ID=default ...
- 设置IE浏览器代理上网
在局域网中,服务器可以直接通过IE网上冲浪,而工作站要想通过IE上网,如果是在服务器使用代理软件的情况下,其IE需要设置代理. 步骤一:启动IE浏览器,选择"工具",再" ...
- iOS CAShapeLayer精讲
前言 CAShapeLayer继承自CALayer,因此,可使用CALayer的所有属性.但是,CAShapeLayer需要和贝塞尔曲线配合使用才有意义. 关于UIBezierPath,请阅读文章:i ...