如何从模型中获取Zend \ Db \ Adapter实例? (ZF2) [英] How to get Zend\Db\Adapter instance from within a Model? (ZF2)

查看:81
本文介绍了如何从模型中获取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.

这篇关于如何从模型中获取Zend \ Db \ Adapter实例? (ZF2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆