1. Home
  2. Documentation
  3. Tutorials
  4. Component Tutorials
  5. Setting Up A Database Adapter

Component Tutorials

In This Article

Setting up a Database Adapter

zend-db provides a general purpose database abstraction layer. At its heart istheAdapter, which abstracts common database operations across the variety ofdrivers we support.

In this guide, we will document how to configure both a single, default adapteras well as multiple adapters (which may be useful in architectures that have acluster 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

Installation and automated Configuration

If you are usingzend-component-installer(installed by default with the skeleton application, and optionally forExpressive applications), you will be prompted to install the packageconfiguration.

  • For zend-mvc applications, choose eitherapplication.config.php ormodules.config.php.
  • For Expressive applications, chooseconfig/config.php.

Installation and manual Configuration

If you are not using the installer, you will need to manually configure add thecomponent to your application.

Configuration for a zend-mvc-based Application

For zend-mvc applications, update your list of modules in eitherconfig/application.config.php orconfig/modules.config.php to add anentry for'Zend\Db' at the top of the list:

// In config/modules.config.phpreturn [    'Zend\Db', // <-- This line    'Zend\Form',     /* ... */];// OR in config/application.config.phpreturn [    /* ... */    // Retrieve list of modules used in this application.    'modules' => [        'Zend\Db', // <-- This line        'Zend\Form',         /* ... */    ],    /* ... */];

Configuration for a zend-expressive-based Application

For Expressive applications, create a new file,config/autoload/zend-db.global.php, with the following contents:

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 theclass nameZend\Db\Adapter\AdapterInterface:

use Zend\Db\Adapter\AdapterInterface;function ($container) {    return new SomeServiceObject($container->get(AdapterInterface::class));}

When installed and configured, the factory associated withAdapterInterfacewill look for a top-leveldb key in the configuration, and use it to create anadapter. As an example, the following would connect to a MySQL database usingPDO, and the supplied PDO DSN:

// In config/autoload/global.phpreturn [    'db' => [        'driver' => 'Pdo',        'dsn'    => 'mysql:dbname=zftutorial;host=localhost;charset=utf8',    ],];

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 acluster of databases, one may allow write operations, while another may beread-only.

zend-db provides anabstract factory,Zend\Db\Adapter\AdapterAbstractServiceFactory, for this purpose. To use it,you will need to create named configuration keys underdb.adapters, each withconfiguration for an adapter:

// In config/autoload/global.phpreturn [    'db' => [        'adapters' => [            'Application\Db\WriteAdapter' => [                'driver' => 'Pdo',                'dsn'    => 'mysql:dbname=application;host=canonical.example.com;charset=utf8',            ],            'Application\Db\ReadOnlyAdapter' => [                'driver' => 'Pdo',                'dsn'    => 'mysql:dbname=application;host=replica.example.com;charset=utf8',            ],        ],    ],];

You retrieve the database adapters using the keys you define, so ensure they areunique to your application, and descriptive of their purpose!

Retrieving named Adapters

Retrieve named adapters in your service factories just as you would anotherservice:

function ($container) {    return new SomeServiceObject($container->get('Application\Db\ReadOnlyAdapter'));}

Using theAdapterAbstractServiceFactory as a Factory

Depending on what application container you use, abstract factories may not beavailable. Alternately, you may want to reduce lookup time when retrieving anadapter from the container (abstract factories are consulted last!).zend-servicemanager abstract factories work as factories in their own right, andare passed the service name as an argument, allowing them to vary their returnvalue based on requested service name. As such, you can add the followingservice configuration as well:

use Zend\Db\Adapter\AdapterAbstractServiceFactory;// If using zend-mvc:// In module/YourModule/config/module.config.php'service_manager' => [    'factories' => [        'Application\Db\WriteAdapter' => AdapterAbstractServiceFactory::class,    ],],// If using Expressive'dependencies' => [    'factories' => [        'Application\Db\WriteAdapter' => AdapterAbstractServiceFactory::class,    ],],

Found a mistake or want to contribute to the documentation? Edit this page on GitHub!