Setting up a database adapter

Introduction

In most cases, e.g. in your controllers, your database adapter can be fetched directly from the service manager. Some classes however, like Zend\Validator\DbRecordExists isn’t aware of the service manager, but still needs an adapter to function.

There are many different ways to provide this functionality to your application. Below are a few examples.

Basic setup

Normally you will setup your database adapter using a factory in the service manager in your configuration. It might look something like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// config/autoload/global.php

return array(
   'db' => array(
      'driver'         => 'Pdo',
      'dsn'            => 'mysql:dbname=zf2tutorial;host=localhost',
   ),
   'service_manager' => array(
      'factories' => array(
         'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
      ),
   ),
);

The adapter can then be accessed in any ServiceLocatorAware classes.

1
2
3
4
5
6
7
8
public function getAdapter()
{
   if (!$this->adapter) {
      $sm = $this->getServiceLocator();
      $this->adapter = $sm->get('Zend\Db\Adapter\Adapter');
   }
   return $this->adapter;
}

More information on adapter options can be found in the docs for Zend\Db\Adapter.

Setting a static adapter

In order to utilize this adapter in non-ServiceLocatorAware classes, you can use Zend\Db\TableGateway\Feature\GlobalAdapterFeature::setStaticAdapter() to set a static adapter:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
// config/autoload/global.php

return array(
   'db' => array(
      'driver'         => 'Pdo',
      'dsn'            => 'mysql:dbname=zf2tutorial;host=localhost',
   ),
   'service_manager' => array(
      'factories' => array(
         'Zend\Db\Adapter\Adapter' => function ($serviceManager) {
            $adapterFactory = new Zend\Db\Adapter\AdapterServiceFactory();
               $adapter = $adapterFactory->createService($serviceManager);

               \Zend\Db\TableGateway\Feature\GlobalAdapterFeature::setStaticAdapter($adapter);

               return $adapter;
         }
      ),
   ),
);

The adapter can then later be fetched using Zend\Db\TableGateway\Feature\GlobalAdapterFeature::getStaticAdapter() for use in e.g. Zend\Validator\DbRecordExists:

1
2
3
4
5
6
7
$validator = new Zend\Validator\Db\RecordExists(
   array(
      'table'   => 'users',
      'field'   => 'emailaddress',
      'adapter' => \Zend\Db\TableGateway\Feature\GlobalAdapterFeature::getStaticAdapter()
   )
);

Table Of Contents

Previous topic

Using Zend\Paginator in your Album Module

Next topic

Migration from Zend Framework 1

This Page

Note: You need to stay logged into your GitHub account to contribute to the documentation.

Edit this document

Edit this document

The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.

  1. Login with your GitHub account.
  2. Go to Setting up a database adapter on GitHub.
  3. Edit file contents using GitHub's text editor in your web browser
  4. Fill in the Commit message text box at the end of the page telling why you did the changes. Press Propose file change button next to it when done.
  5. On Send a pull request page you don't need to fill in text anymore. Just press Send pull request button.
  6. Your changes are now queued for review under project's Pull requests tab on GitHub.