1

I want to built Doctrine 2 into an Apigility Zend Framework 2 application.

Following Marco Pivetta's Doctrine ORM ZF2 Tutorial, I installed not only Doctrine, but also the Zend Developer Tools, like in the tutorial shown:

$ 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
    ),
    // [...]
);

Now when I open the Apigility admin page, I get errors:

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

The errors occur, when the zenddevelopertools.toolbar.enabled is set to true.

Here is the configuration in the /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(
        ...
    ),
);

and the /config/autoload/local.php looks like follows:

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(
        ...
    ),
);

I guess, the Zend Developer Tools cannot find an appropriate DB adapter.

Why are these errors occuring and how to ix them?


Additional info:

The db.service_manager.factories.Zend\\Db\\Adapter\\Adapter configuration in my global.php is actually needless, since I use custom DB adapters instead of the default one created by Zend\Db\Adapter\AdapterServiceFactory. Anyway the Zend Developer Tools seem to try to use it. So, when I copy the DB settings directly to db

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

I get different errors:

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
automatix
  • 14,018
  • 26
  • 105
  • 230

1 Answers1

4

You are only showing Db configs. You need a Doctrine config something like this:

<?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',
                )
            )
        )
    ),
);

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

automatix
  • 14,018
  • 26
  • 105
  • 230
dualmon
  • 1,225
  • 1
  • 8
  • 16
  • It works, thank you very much! But the ZDT don't need Doctrine. I've even disabled Doctrine (`DoctrineModule` and `DoctrineORMModule`) in the module list of the `application.config.php` and it's still working without it. Could you please explain, why does the doctrine DB connection need to be configured for the Zend Developer Tools? – automatix Jan 29 '15 at 14:59
  • 1
    I think after you comment out the Doctrine modules from application.config.php ZDT won't need the doctrine config any more. If it does, I'm not sure why. – dualmon Jan 29 '15 at 15:07
  • Yes, you are right. It's working now! Thank you! Another problem is, that my other DB connection configs are ignored now, so I cannot switch between the versions. But it's another topic. I started a new question here: http://stackoverflow.com/q/28218959/2019043 – automatix Jan 29 '15 at 16:05
  • I corrected the question (made it more precise) and moved the part about the usage of the Zend Developer Tools with custom/multiple DB adapters to a new one: http://stackoverflow.com/q/28227096/2019043. – automatix Jan 30 '15 at 08:52