【问题标题】:How to get Zend\Db\Adapter instance from within a Model? (ZF2)如何从模型中获取 Zend\Db\Adapter 实例? (ZF2)
【发布时间】:2012-05-02 12:45:35
【问题描述】:

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

如何将适配器的主实例拉到我的ManagerAbstract?在 ZF1 中,我可以使用 Zend_Registry 实现这一点。

如果这不是 ZF2 中做事的正确方式,我很想听听这种事情的正确方式。

谢谢!

【问题讨论】:

    标签: php model-view-controller zend-framework zend-framework2


    【解决方案1】:

    使用依赖注入容器Zend\Di。 ZfcUser 项目会执行此操作,如果您想查看一些工作代码。

    或者,基本方法是这样的(代码未经测试!):

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

    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 文件中,将适配器注入映射器:

    模块/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 类可以接收注入:

    模块/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 检索它或将映射器注入服务,然后将服务注入您要使用它的控制器(或其他服务)。

    【讨论】:

    • 哇,感谢您的精彩回答!不过,我仍然有一个我无法弄清楚的问题。 Missing instance/object for parameter driver for Zend\Db\Adapter\Adapter::__construct - 可能是什么问题?
    • 这意味着 DI 配置在某处是错误的。可能在 Zend\Db\Adapter\Adapter 的定义内
    猜你喜欢
    • 2023-04-01
    • 1970-01-01
    • 2016-03-17
    • 1970-01-01
    • 1970-01-01
    • 2017-06-27
    • 1970-01-01
    • 2023-03-29
    • 2023-03-23
    相关资源
    最近更新 更多