Doctrine validate-schema PDOexception

2019-09-17 10:13发布

问题:

When I do this step on marco pivetta tutorial Validate-schema I have this error :

[PDOException]
SQLSTATE[HY000] [1045] Access denied for user 'username'@'localhost' (using password: YES)

The problem is, on line command i think my doctrine.local.php

return array(
'doctrine' => array(
        'connection' => array(
          'orm_default' => array(
                'driverClass' =>'Doctrine\DBAL\Driver\PDOMySql\Driver',
                'params' => array(
                  'host'     => 'localhost',
                  'port'     => '3306',
                  'user'     => 'root',
                  'password' => '',
                  'dbname'   => 'test',
                  'charset' => 'utf8',
                  'driverOptions' => array (1002 => 'SET NAMES utf8'),
                )
            )
        )
    )
);

is not loaded for this tool. My $params is empty (Doctrine\DBAL\Driver\PDOMySql\Driver) And _constructPdoDsn return standard $dsn, not my configs params.

My Os is windows 7 I'm working with Zend Framework2 on wamp and I use Console2 with gitBash for command line.

I'm stuck any help please ?

回答1:

You need to have cli-config.php in your root.

Here is content of mine:

<?php
date_default_timezone_set('Europe/Prague');

use Zend\Loader\AutoloaderFactory;
use Zend\Mvc\Service\ServiceManagerConfig;
use Zend\ServiceManager\ServiceManager;
use Zend\Stdlib\ArrayUtils;
chdir(__DIR__);

class Bootstrap
{
protected static $serviceManager;
protected static $config;
protected static $bootstrap;

public static function init()
{
    // Load the user-defined test configuration file, if it exists; otherwise, load
    if (is_readable(__DIR__ . '/config/application.config.php')) {
        $testConfig = include __DIR__ . '/config/application.config.php';
    } else {
        //$testConfig = include __DIR__ . '/TestConfig.php.dist';
    }

    $zf2ModulePaths = array();

    if (isset($testConfig['module_listener_options']['module_paths'])) {
        $modulePaths = $testConfig['module_listener_options']['module_paths'];
        foreach ($modulePaths as $modulePath) {
            if (($path = static::findParentPath($modulePath)) ) {
                $zf2ModulePaths[] = $path;
            }
        }
    }

    $zf2ModulePaths  = implode(PATH_SEPARATOR, $zf2ModulePaths) . PATH_SEPARATOR;
    $zf2ModulePaths .= getenv('ZF2_MODULES_TEST_PATHS') ?: (defined('ZF2_MODULES_TEST_PATHS') ? ZF2_MODULES_TEST_PATHS : '');

    static::initAutoloader();

    // use ModuleManager to load this module and it's dependencies
    $baseConfig = array(
            'module_listener_options' => array(
                    'module_paths' => explode(PATH_SEPARATOR, $zf2ModulePaths),
            ),
    );

    $config = ArrayUtils::merge($baseConfig, $testConfig);

    $serviceManager = new ServiceManager(new ServiceManagerConfig());
    $serviceManager->setService('ApplicationConfig', $config);
    $serviceManager->get('ModuleManager')->loadModules();

    \Doctrine\ORM\Tools\Console\ConsoleRunner::run(
            new \Symfony\Component\Console\Helper\HelperSet(
                    array(
                            'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($serviceManager->get('Doctrine\ORM\EntityManager'))
                    )
            )
    );

    static::$serviceManager = $serviceManager;
    static::$config = $config;
}

public static function getServiceManager()
{
    return static::$serviceManager;
}

public static function getConfig()
{
    return static::$config;
}

protected static function initAutoloader()
{
    $vendorPath = static::findParentPath('vendor');

    if (is_readable($vendorPath . '/autoload.php')) {
        $loader = include $vendorPath . '/autoload.php';
    } else {
        $zf2Path = getenv('ZF2_PATH') ?: (defined('ZF2_PATH') ? ZF2_PATH : (is_dir($vendorPath . '/ZF2/library') ? $vendorPath . '/ZF2/library' : false));

        if (!$zf2Path) {
            throw new RuntimeException('Unable to load ZF2. Run `php composer.phar install` or define a ZF2_PATH environment variable.');
        }

        include $zf2Path . '/Zend/Loader/AutoloaderFactory.php';

    }

    AutoloaderFactory::factory(array(
    'Zend\Loader\StandardAutoloader' => array(
    'autoregister_zf' => true,
    'namespaces' => array(
    __NAMESPACE__ => __DIR__ . '/' . __NAMESPACE__,
    ),
    ),
    ));
}

protected static function findParentPath($path)
{
    $dir = __DIR__;
    $previousDir = '.';
    while (!is_dir($dir . '/' . $path)) {
        $dir = dirname($dir);
        if ($previousDir === $dir) return false;
        $previousDir = $dir;
    }
    return $dir . '/' . $path;
}
}

Bootstrap::init();


回答2:

I found solution....

On windows seven, we have to use this line command (or we can also do this command via index.php in public dir since zf2 2.3.0)

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

And, if you use gitBash don't forget if you have tested your APPLICATION_ENV variable in application.config.php like this tutorial Zf2 advances config setup do in bash_profile file :

export APPLICATION_ENV="development"

Hope this help someone. You don't need to have cli-config.php in the root directory to do that.