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

时间:2015-01-28 16:11:18

标签: php doctrine-orm zend-framework2 apigility zend-tool

我想在Doctrine Apigility 2个应用中构建Zend Framework 2。

关注Marco PivettaDoctrine ORM ZF2 Tutorial,我不仅安装了Doctrine,还安装了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找不到合适的数据库适配器。

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


其他信息:

db.service_manager.factories.Zend\\Db\\Adapter\\Adapter中的global.php配置实际上是不必要的,因为我使用自定义数据库适配器而不是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

1 个答案:

答案 0 :(得分:4)

您只显示Db配置。你需要一个像这样的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