Welcome to Abdul Malik Ikhsan's Blog

Zend Framework 2 : Setting Default Db Adapter via Service initializers

Posted in Tutorial PHP, Zend Framework 2 by samsonasik on November 13, 2012

I just read very useful tutorial from akrabat site. He explain about Zend\ServiceManager configuration keys. And, the most I like is about service initializer. It can inject to all instance of something, for example, if Model is extends AbstractTableGateway.
For example, you have SampleTable class like the following :

class SampleTable extends AbstractTableGateway
      implements \Zend\Db\Adapter\AdapterAwareInterface 
    protected $table = 'sampletable';
    public function setDbAdapter(Adapter $adapter)
        $this->adapter = $adapter;
        $this->resultSetPrototype = new HydratingResultSet();
        $this->resultSetPrototype->setObjectPrototype(new Sample());

So, you can initialize of instantiation of all class that implement AdapterAwareInterface like SampleTable.

class Module 
    public function getServiceConfig()
	return array(
	    'initializers' => array(
		function ($instance, $sm) {
		    if ($instance instanceof \Zend\Db\Adapter\AdapterAwareInterface) {
            'factories' => array(
                'SampleModule\Model\SampleTable' =>  function($sm){
		    $table     = new Model\SampleTable();
		    return $table;

Easy setup, thanks Akrabat.

Reference :

11 Responses

Subscribe to comments with RSS.

  1. Ralf Eggert said, on November 13, 2012 at 3:33 pm

    Nice one. You could even improve the example a little by using the Zend\Db\Adapter\AdapterAwareInterface.

  2. Michaël Gallego said, on November 13, 2012 at 4:04 pm

    As Ocramius said to me one day, initializers can be dangerous especially with hard dependencies like this. Nothing disallow a user to directly instantiate the object… And boom.

    In this case, I think moving the dependency to the constructor and using an abstract factory may be a better idea.

    • samsonasik said, on November 13, 2012 at 5:00 pm

      I just follow akrabat said ” Another really common use-case is injecting a database adapter and Zend Framework supplies Zend\Db\Adapter\AdapterAwareInterface for this case”. I just updated my post based on Ralf Eggert suggestion to using Zend\Db\Adapter\AdapterAwareInterface. Can you give me an example to use abstract factory for this case ? I think it will helpful to improve this post.

      Thank you very much.

  3. Marcelo said, on November 13, 2012 at 11:46 pm

    To get a parameter set in getServiceConfig ()
      I use the controller: $ this-> serviceLocator-> get (‘ParameterName’).
    My doubt is how do I get to the same parameter form of a class or a class model … is that possible?

  4. […] Mahlik Ikhsan sorgt dafür, dass das DbAdapaterAwareInterface auch so funktioniert, wie man es erwarten […]

  5. Wilmer said, on January 25, 2016 at 9:49 pm

    I use doctrine orm in a big project, I’m using initializer to inject entity manager to a super class repository (using interface); in question of performance, this option is better than inject entity manager to each repository class. My concern is because the initializer must validate each instance.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: