【问题标题】:Doctrine validate-schema PDOexceptionDoctrine validate-schema PDOexception
【发布时间】:2014-03-26 13:01:53
【问题描述】:

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

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

问题是,在线命令我认为我的学说.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,而不是我的配置参数。

我的操作系统是 windows 7 我正在 wamp 上使用 Zend Framework2 我使用 Console2 和 gitBash 作为命令行。

我有什么帮助吗?

【问题讨论】:

  • 不,这个文件甚至没有被读取。无论我在那里写什么......我什至在 PHPmyAdmin 上创建了一个具有相同学说配置的用户,但运气不好。

标签: doctrine-orm console zend-framework2


【解决方案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();

【讨论】:

  • 感谢您的回答,但即使这是一个可能的解决方案,我仍然有同样的问题(我的 cli-config 确实不在根目录中)。你知道为什么吗?
  • 抱歉,这不是解决方案。无论如何,谢谢你给了我很好的建议来找出这个问题。
【解决方案2】:

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

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

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 来做到这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-13
    • 2014-07-21
    • 1970-01-01
    • 1970-01-01
    • 2011-10-18
    • 1970-01-01
    • 1970-01-01
    • 2013-05-03
    相关资源
    最近更新 更多