简体   繁体   English

Zend Framework 2:数据库配置

[英]Zend Framework 2: Database config

I'm digging into ZF2, and I've run into some confusion on how to use Zend\\Config with Zend\\Db to manually set up a connection. 我正在研究ZF2,并且在如何将Zend \\ Config与Zend \\ Db一起使用以手动建立连接方面遇到了一些困惑。

In different places in the manual, there are db configs in different formats. 在手册的不同位置,都有不同格式的数据库配置。

This one shows a flat array as the config format: https://packages.zendframework.com/docs/latest/manual/en/modules/zend.db.adapter.html 此示例显示了一个平面数组作为配置格式: https : //packages.zendframework.com/docs/latest/manual/en/modules/zend.db.adapter.html

$adapter = new Zend\Db\Adapter\Adapter(array(
    'driver' => 'Mysqli',
    'database' => 'zend_db_example',
    'username' => 'developer',
    'password' => 'developer-password'
));

While this one shows a nested format: https://packages.zendframework.com/docs/latest/manual/en/modules/zend.config.introduction.html 尽管此格式显示了一种嵌套格式: https : //packages.zendframework.com/docs/latest/manual/en/modules/zend.config.introduction.html

$configArray = array(
    'database' => array(
        'adapter' => 'pdo_mysql',
        'params'  => array(
            'host'     => 'db.example.com',
            'username' => 'dbuser',
            'password' => 'secret',
            'dbname'   => 'mydatabase'
        )
    )
);

What I expect to happen is that I can call for a new db adapter like so, but this throws exceptions: 我期望发生的事情是我可以像这样调用新的数据库适配器,但这会引发异常:

$config = new Zend\Config\Config(
    array(
        'db' => array(
            'adapter' => 'Mysqli',
            'params'  => array(
                'host'     => 'db.example.com',
                'username' => 'dbuser',
                'password' => 'secret',
                'dbname'   => 'mydatabase'
            )
        )
    )
);

$adapter = new Zend\Db\Adapter\Adapter($config->db);

What I end up having to do is: 我最终要做的是:

$config = new Zend\Config\Config(
    array(
        'db' => array(
            'driver' => 'Mysqli',
            'host'     => 'db.example.com',
            'username' => 'dbuser',
            'password' => 'secret',
            'database'   => 'mydatabase'
        )
    )
);

$adapter = new Zend\Db\Adapter\Adapter($config->db->toArray());

Is there a better way of achieving what I'm trying to achieve without having to resort to the service manager? 是否有更好的方式来实现我要实现的目标而不必求助于服务经理?

Ignore the example from the Zend Config introduction page, that's just showing how to make a config object from a PHP array, the structure of the array isn't meant to show anything in particular. 忽略Zend Config简介页面中的示例,该示例仅显示如何从PHP数组制作配置对象,该数组的结构并不意味着要特别显示任何内容。

Since you don't want to use the service manager, you need to pass the parameters to the adapter class in the structure it expects. 由于您不想使用服务管理器,因此需要将参数传递给期望的结构中的适配器类。 It expects an array, a config object won't work. 它期望一个数组,一个配置对象将不起作用。 You've worked out what the structure of the array is, so that's what you need to use. 您已经弄清了数组的结构是什么,因此您需要使用它。

I think this page in the docs: http://framework.zend.com/manual/2.3/en/tutorials/tutorial.dbadapter.html (the "Basic setup" section) gives a better explanation of the service manager approach, which is how I'd do it in an MVC app at least. 我认为文档中的此页面: http : //framework.zend.com/manual/2.3/en/tutorials/tutorial.dbadapter.html (“基本设置”部分)提供了对服务管理器方法的更好解释,该方法至少我是在MVC应用程序中做到这一点的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM