2

I'm creating abstract models for managing database entities - I already have EntityAbstract, EntitySetAbstract and a ManagerAbstract models. In my ManagerAbstract model I need a Zend/Db/Adapter instance in order to create a Zend\Db\TableGateway.

How could I pull the main instance of the adapter to my ManagerAbstract? In ZF1 I could have achieved this with Zend_Registry.

If this isn't the right way of doing things in ZF2, I would love to hear the correct way to this kind of things.

Thanks!

1 Answer 1

7

Use the Dependency Injection Container, Zend\Di. The ZfcUser project does this if you want to poke around in some working code.

Alternatively, the basic approach is something like this (code untested!):

Firstly: configure the DI to inject the database connection information:

config/autoload/local.config.php:

<?php
return array(
    'di' => array(
        'instance' => array(
        'Zend\Db\Adapter\Adapter' => array(
                'parameters' => array(
                    'driver' => 'Zend\Db\Adapter\Driver\Pdo\Pdo',
                ),
            ),
            'Zend\Db\Adapter\Driver\Pdo\Pdo' => array(
                'parameters' => array(
                    'connection' => 'Zend\Db\Adapter\Driver\Pdo\Connection',
                ),
            ),
            'Zend\Db\Adapter\Driver\Pdo\Connection' => array(
                'parameters' => array(
                    'connectionInfo' => array(
                        'dsn'            => "mysql:dbname=mydatabasename;host=localhost",
                        'username'       => 'myusername',
                        'password'       => 'mypassword',
                        'driver_options' => array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''),
                    ),
                ),
            ),
        ),
    ),
);

Secondly, within your module's module.config.php file, inject the adapter into the mapper:

module/My/config/module.config.php:

<?php
return array(
    'di' => array(

            // some config info...

            'My\Model\ManagerAbstract' => array(
                'parameters' => array(
                    'adapter'  => 'Zend\Db\Adapter\Adapter',
                ),
            ),

            // more config info...
    )
);

Finally, ensure that your ManagerAbstract class can receive the injection:

module/My/src/My/Model/ManagerAbstract.php:

<?php
namespace My\Model;

use Zend\Db\Adapter\Adapter;
use Zend\Db\Adapter\AdapterAwareInterface;

abstract class ManagerAbstract implements AdapterAwareInterface 
{
    /**
     * @var Zend\Db\Adapter\Adapter
     */
    protected $adapter;

    // some code 

    public function setDbAdapter(Adapter $adapter)
    {
        $this->adapter = $adapter;
    }

    // some more code
}

Note that to use any sub-class, you need to retrieve it via the DIC or inject the mapper into the service and then inject the service into the controller (or other service) where you want to use it.

Sign up to request clarification or add additional context in comments.

2 Comments

Wow, thanks for the amazing answer! though, I'm still having a problem I couldn't manage to figure out. Missing instance/object for parameter driver for Zend\Db\Adapter\Adapter::__construct - what could be the issue?
That implies that the DI configuration is wrong somewhere. Probably within the definition of Zend\Db\Adapter\Adapter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.