Doctrine 2 Console Tool说:你错过了“cli-config.php”或“config / cli-config.php”

时间:2013-11-22 14:31:36

标签: doctrine-orm console doctrine configuration-files

我运行了doctrine控制台工具:

$ php vendor/doctrine/orm/bin/doctrine orm:schema-tool:create --dump-sql

我得到了这个而不是预期的功能:

You are missing a "cli-config.php" or "config/cli-config.php" file in your
project, which is required to get the Doctrine Console working. You can use the
following sample as a template:

<?php
use Doctrine\ORM\Tools\Console\ConsoleRunner;

// replace with file to your own project bootstrap
require_once 'bootstrap.php';

// replace with mechanism to retrieve EntityManager in your app
$entityManager = GetEntityManager();

return ConsoleRunner::createHelperSet($entityManager);

的问题:

  • 我在ZF2上,没有名为bootstrap.php
  • 的文件
  • 我是ZF2的新手,所以我不知道我的entityManager是什么以及我应该为GetEntityManager
  • 添加什么

我如何使这项工作?

3 个答案:

答案 0 :(得分:10)

简易方法

使用Doctrine模块:

vendor/bin/doctrine-module orm:schema-tool:create --dump-sql

答案 1 :(得分:2)

困难之路

您需要自己编写两个文件:bootstrap.phpcli-config.php

为什么我要写两个文件,呵呵?

Doctrine Docs指示您这样做。

它在哪里这样说?

下面:

在cli-config中,查找代码

// replace with mechanism to retrieve EntityManager in your app
$entityManager = GetEntityManager();

在这里,GetEntityManager()不是任何库或代码的一部分,它是您自己的自定义方法,Doctrine建议您自己编写,其中该方法返回Doctrine\ORM\EntityManager的实例。我认为这一部分的学说信息可能更加明确。

我仍然无法做到,文档过于苛刻,请帮忙!

如果您的配置中有太多图层,即尝试使用Symphony的ZF2编制Doctrine,那么可能很难确定Doctrine在框架启动的哪个位置结束等等。尝试单独设置Doctrine测试容器,遵循Doctrine文档,不使用任何框架,事情应该变得非常清晰。

这是我为Doctrine配置所做的。我选择创建3个文件以获得灵活性,并将它们全部放入我的Zend文件夹结构的./config文件夹中。

首先,我有cli-config.php

use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Console\ConsoleRunner;
require 'config/bootstrap.php';

$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$entityManager = EntityManager::create($dbParams, $config);
return ConsoleRunner::createHelperSet($entityManager);

然后,我的bootstrap.php

date_default_timezone_set("America/Detroit"); // Set the default timezone
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(- 1);

include 'vendor/autoload.php';
include 'doctrine-config.php';

最后,我的doctrine-config.php。它是分开的,并且很容易从代码的其他部分调用此文件。在我的情况下,当我需要Doctrine连接时,这将被调用CLI并从ZF2单例中调用。

// Paths to Entities that we want Doctrine to see
$paths = array(
    "module/Module/src/Entity",
    "module/MyApplication/src/Entity"
);

// Tells Doctrine what mode we want
$isDevMode = true;

// Doctrine connection configuration
$dbParams = array(
    'driver' => 'pdo_mysql',
    'user' => 'user',
    'password' => 'pass',
    'dbname' => 'db_name'
);

答案 2 :(得分:0)

对于那些不使用Zend Framework的人

我想使用Doctrine控制台工具来验证我正在开发的Symfony Bundle中的映射,而不必创建整个Symfony应用程序来测试我的工作。我能够按照官方安装指南进行操作 Here

注释:尽管我只是想验证映射,但Doctrine仍需要连接到真实的数据库才能运行。就个人而言,使用pdo_pgsql,除了本指南中的内容外,我还需要使用hostport DBParams。