如何从模型中获取Zend \ Db \ Adapter实例? (ZF2)

问题描述:

我正在创建用于管理数据库实体的抽象模型-我已经具有EntityAbstractEntitySetAbstractManagerAbstract模型.在我的ManagerAbstract模型中,我需要一个Zend/Db/Adapter实例才能创建一个Zend\Db\TableGateway.

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.

如何将适配器的主实例拉到我的ManagerAbstract?在ZF1中,我可以通过Zend_Registry实现这一目标.

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

如果这不是ZF2中正确的处理方式,我很想听听解决此类问题的正确方法.

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.

谢谢!

使用依赖注入容器Zend\Di.如果您想在一些工作代码中浏览,请 ZfcUser 项目执行此操作.

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!):

首先:配置DI以注入数据库连接信息:

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\''),
                    ),
                ),
            ),
        ),
    ),
);

第二,在模块的module.config.php文件中,将适配器注入映射器:

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...
    )
);

最后,确保您的ManagerAbstract类可以接收注入:

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
}

请注意,要使用任何子类,您需要通过DIC检索它或将映射器注入服务中,然后将服务注入要使用它的控制器(或其他服务)中.

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.