How to get Zend\\Db\\Adapter instance from within

2020-05-24 18:04发布

问题:

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:

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.