Uncaught PDOException while using Doctrine 2 CLI in ZF2

1.3k views Asked by At

I am getting an uncaught PDO Exception while trying to use the doctrine CLI. I am following marco's tutorial.

My configuration is : ZF 2.5.1 with PHP version 5.5.21

Zend Server 8.0.2

Exception Encountered:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] No such file or directory' in /usr/local/zend/apache2/htdocs/website-/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:43 Stack trace: 0. /usr/local/zend/apache2/htdocs/website-/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php(43): PDO->__construct('mysql:host=loca...', 'root', '', Array) 1. /usr/local/zend/apache2/htdocs/website-/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOMySql/Driver.php(45): Doctrine\DBAL\Driver\PDOConnection->__construct('mysql:host=loca...', 'root', '', Array) 2. /usr/local/zend/apache2/htdocs/website-/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php(360): Doctrine\DBAL\Driver\PDOMySql\Driver->connect(Array, 'root', '', Array) 3. /usr/local/zend/apache2/htdocs/website-/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php(429): Doctrine\DBAL\Connection->connect() 4. /usr/local/zend/apache2/htdocs/website-/vendor/doctrine/dba in /usr/local/zend/apache2/htdocs/website-/vendor/zendframework/zend-servicemanager/src/ServiceManager.php on line 946


Can anyone help or give me any suggestion please?

Additional Information:

  1. Command used for accessing Doctrine CLI:

    ./vendor/bin/doctrine-module orm:validate-schema

  2. Contents of /vendor/bin/:

ABHI staff 57B Jun 6 00:47 classmap_generator.php -> ../zendframework/zendframework/bin/classmap_generator.php ABHI staff 28B Jun 16 19:46 doctrine -> ../doctrine/orm/bin/doctrine ABHI staff 34B Jun 16 19:46 doctrine-dbal -> ../doctrine/dbal/bin/doctrine-dbal ABHI staff 47B Jun 16 19:46 doctrine-module -> ../doctrine/doctrine-module/bin/doctrine-module ABHI staff 32B Jun 16 19:46 doctrine.php -> ../doctrine/orm/bin/doctrine.php root staff 26B Jun 16 20:26 phpunit -> ../phpunit/phpunit/phpunit ABHI staff 58B Jun 6 00:47 pluginmap_generator.php -> ../zendframework/zendframework/bin/pluginmap_generator.php ABHI staff 60B Jun 6 00:47 templatemap_generator.php -> ../zendframework/zendframework/bin/templatemap_generator.php ABHI staff 30B Jun 16 19:52 zf.php -> ../zendframework/zftool/zf.php

  1. Snippet from doctrine-module file:

    /* @var $cli \Symfony\Component\Console\Application */
    $cli = $application->getServiceManager()->get('doctrine.cli');
    exit($cli->run());
    
  2. Throwing error in the following function in the PDOConnection.php file:

    public function __construct($dsn, $user = null, $password = null, array $options = null)
    {
        try {
            parent::__construct($dsn, $user, $password, $options);
            $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('Doctrine\DBAL\Driver\PDOStatement', array()));
            $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        } catch (\PDOException $exception) {
            throw new PDOException($exception);
        }
    }
    
1

There are 1 answers

0
awoyotoyin On

I know it's been a while back now since this question was asked but, thought i'd share a solution still since none was provided.

The error suggests your application can not connect to the database, no database connection found to be precise.

If you have the app hosted and testing on a platform somewhere other than your local instance, then you probably pushed using GIT and by default, zf2 has a .gitignore file in the config/autoload folder that ignores the local.php and all other files in that folder with a .local.php extension. Since you are using DoctrineORM, though not a requirement but, I'd guess you probably have a doctrine.local.php file in the config/autoload folder with your database connection.

Fixes:

  1. You could use ftp instead to upload the database connection file local.php or any other file that carries your database connection.

  2. You could remove the local.php file from the .gitignore file that probably has your database connection details.

  3. You could use the global.php

Hope this helps someone who may face similar issue later on.