Doctrine validate-schema PDOexception

时间:2014-03-26 13:01:53

标签: doctrine-orm console zend-framework2

当我在marco pivetta教程上执行此步骤时 Validate-schema 我有这个错误:

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

问题是,在线命令我认为我的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'),
                )
            )
        )
    )
);
此工具未加载

。我的$ params是空的(Doctrine \ DBAL \ Driver \ PDOMySql \ Driver)而_constructPdoDsn返回标准$ dsn,而不是我的configs params。

我的Os是Windows 7 我在wamp上使用Zend Framework2 我使用Console2和gitBash作为命令行。

我有什么帮助吗?

2 个答案:

答案 0 :(得分:1)

您需要在根目录中拥有cli-config.php

以下是我的内容:

<?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();

答案 1 :(得分:0)

我找到了解决方案......

在Windows 7上,我们必须使用此行命令(或者我们也可以通过zf2 2.3.0中的公共目录中的index.php执行此命令)

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

并且,如果您使用gitBash,请不要忘记您是否已在application.config.php中测试过您的APPLICATION_ENV变量,如本教程 Zf2 advances config setup 在bash_profile文件中执行:

export APPLICATION_ENV="development"

希望这能有所帮助。 您不需要在根目录中使用cli-config.php来执行此操作。