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

Den*_*nis 8 console doctrine configuration-files doctrine-orm

我运行了doctrine控制台工具:

$ php vendor/doctrine/orm/bin/doctrine orm:schema-tool:create --dump-sql
Run Code Online (Sandbox Code Playgroud)

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

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);
Run Code Online (Sandbox Code Playgroud)

问题:

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

我该如何工作?

Den*_*nis 11

简单的方法

使用Doctrine模块:

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

  • 我的问题中的Doctrine Console工具需要一个具有完全定义的实体管理器的环境才能工作.`doctrine-module`工具必须使用其他东西,或者完全不同的工具,因为它不需要实体管理器工作.相反,搞清楚如何定义一个实体管理器,或如何定义ZF2环境,以便能够使用原则控制台的架构工具,我选择使用`学说,module`的工具,并没有要求我做任何额外的设置. (2认同)

Den*_*nis 5

艰难的道路

把它放到你的config/cli-config.php

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Console\ConsoleRunner;
use DoctrineConnector\EntityManagerFactory;
include 'vendor/autoload.php'

global $container;
$container = require 'config/container.php';

$factory = new EntityManagerFactory();
$entityManager = $factory($container);
return ConsoleRunner::createHelperSet($entityManager);
Run Code Online (Sandbox Code Playgroud)

把它放到你的config/autoload/database.local.php

return [
   'doctrine' => [
        /*
         * Paths for Doctrine to find Entities
         */
        'paths' => array(
            "src/Blah/src/Entity",
        ),

        /*
         * Doctrine configuration parameters
         */
        'db_params' => array(
            'driver' => 'pdo_mysql',
            'user' => 'user',
            'password' => 'password',
            'dbname' => 'dbname',
            'driverOptions' => array(
                1002 => 'SET NAMES UTF8MB4'
            )
        ),

        /*
         * Tells Doctrine what mode we want
         * For Production environment set to \Doctrine\Common\Proxy\AbstractProxyFactory::AUTOGENERATE_NEVER;
         */
        'is_dev_mode' => false
    ]
];
Run Code Online (Sandbox Code Playgroud)

创建这个类: EntityManagerFactory

class EntityManagerFactory
{

    /**
     * Set Up & return Doctrine connection
     *
     * @param ContainerInterface $container
     * @throws \RuntimeException
     * @return EntityManager
     */
    public function __invoke(ContainerInterface $container): EntityManager
    {
        $config = $container->get('config');
        Assert::that($config)->keyExists('doctrine', "No Doctrine ORM Configuration Specified, check your 'config/autoload/database.local.php'");
        $paths = $config['doctrine']['paths'];
        $dbParams = $config['doctrine']['db_params'];
        $isDevMode = $config['doctrine']['is_dev_mode'];

        $config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
        $config->setProxyDir("temp/proxies");
        $entityManager = EntityManager::create($dbParams, $config);

        /*
         * Enable native prepared statements
         */
        $pdo = $entityManager->getConnection()->getWrappedConnection();
        $pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);

        /*
         * Allow Doctrine to persist boolean types
         */
        Type::overrideType('boolean', BooleanToIntType::class);
        return $entityManager;
    }
}
Run Code Online (Sandbox Code Playgroud)

在那之后,事情应该会很顺利