启用 Zend Developer Tools 工具栏导致数据库连接错误

Enabling Zend Developer Tools toolbar causes database connection errors

我想构建 Doctrine 2 into an Apigility Zend Framework 2 个应用程序。

关注 Marco Pivetta's Doctrine ORM ZF2 Tutorial, I installed not only Doctrine, but also the Zend Developer Tools,如教程所示:

$ composer require doctrine/doctrine-orm-module
$ composer require zendframework/zend-developer-tools:dev-master
$ cp vendor/zendframework/zend-developer-tools/config/zenddevelopertools.local.php.dist config/autoload/zdt.local.php

// config/application.config.php
return array(
    'modules' => array(
        'Application',
        ...
        'ZendDeveloperTools', // <-- added
        'DoctrineModule',     // <-- added
        'DoctrineORMModule',  // <-- added
    ),
    // [...]
);

现在,当我打开 Apigility 管理页面时,出现错误:

Fatal error: Uncaught exception 'Zend\Db\Adapter\Exception\InvalidArgumentException' with message 'createDriver expects a "driver" key to be present inside the parameters' in /var/www/myproject/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php on line 909

Zend\Db\Adapter\Exception\InvalidArgumentException: createDriver expects a "driver" key to be present inside the parameters in /var/www/myproject/vendor/zendframework/zendframework/library/Zend/Db/Adapter/Adapter.php on line 262

Zend\ServiceManager\Exception\ServiceNotCreatedException: An exception was raised while creating "Zend\Db\Adapter\Adapter"; no instance returned in /var/www/myproject/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php on line 909

Zend\ServiceManager\Exception\ServiceNotCreatedException: An exception was raised while creating "ZendDeveloperTools\DbCollector"; no instance returned in /var/www/myproject/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php on line 909

zenddevelopertools.toolbar.enabled 设置为 true 时出现错误。

这里是/config/autoload/global.php中的配置:

return array(
    'db' => array(
        'adapters' => array(
            'DB\Customers' => array(),
            'DB\myproject_v1' => array(),
            'DB\myproject_v2' => array(),
        ),
    ),
    'service_manager' => array(
        'factories' => array(
            'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
        ),
    ),
    'zf-mvc-auth' => array(
        ...
    ),
);

并且 /config/autoload/local.php 如下所示:

return array(
    'db' => array(
        'adapters' => array(
            'DB\Customers' => array(
                'driver' => 'Pdo_Sqlite',
                'database' => 'data/sqlite.db',
            ),
            'DB\myproject_v1' => array(
                'driver' => 'Pdo_Mysql',
                'database' => 'myproject_v1',
                'username' => 'root',
                'password' => 'pwd',
                'hostname' => 'localhost',
                'driver_options' => array(
                    1002 => 'SET NAMES \'UTF8\'',
                ),
            ),
            'DB\myproject_v2' => array(
                'driver' => 'Pdo_Mysql',
                'database' => 'myproject_v2',
                'username' => 'root',
                'password' => 'pwd',
                'hostname' => 'localhost',
                'driver_options' => array(
                    1002 => 'SET NAMES \'UTF8\'',
                ),
            ),
        ),
    ),
    'zf-mvc-auth' => array(
        ...
    ),
);

我猜,Zend Developer Tools 找不到合适的数据库适配器。

为什么会出现这些错误以及如何解决它们?


附加信息:

我的 global.php 中的 db.service_manager.factories.Zend\Db\Adapter\Adapter 配置实际上是不必要的,因为我使用自定义数据库适配器而不是 Zend\Db\Adapter\AdapterServiceFactory 创建的默认适配器。不管怎样,Zend Developer Tools 似乎在尝试使用它。所以,当我将数据库设置直接复制到 db

return array(
    'db' => array(
        'adapters' => array(
             ...
            'driver' => 'Pdo_Mysql',
            'username' => 'root',
            'password' => 'pwd',
            'hostname' => 'localhost',
            'database' => 'myproject_v1',
        ),
    ),
    ...
);

我得到不同的错误:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[28000] [1045] Access denied for user 'username'@'localhost' (using password: YES)' in /var/www/myproject/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php on line 1070

PDOException: SQLSTATE[28000] [1045] Access denied for user 'username'@'localhost' (using password: YES) in /var/www/myproject/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php on line 43

您只显示数据库配置。你需要一个像这样的 Doctrine 配置:

<?php
return array(
    'doctrine' => array(
        'connection' => array(
            // default connection name
            'orm_default' => array(
                'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
                'params' => array(
                    'host'     => 'localhost',
                    'port'     => '3306',
                    'user'     => 'username',
                    'password' => 'password',
                    'dbname'   => 'database',
                )
            )
        )
    ),
);

https://github.com/doctrine/DoctrineORMModule/blob/master/README.md