سؤال

I ran doctrine console tool:

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

I got this instead of expected functionality:

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

Issues:

  • I am on ZF2, and there is no file called bootstrap.php
  • I am new to ZF2, so I don't know what my entityManager is and what I should put for GetEntityManager

How do I make this work?

هل كانت مفيدة؟

المحلول

The Easy Way

Use Doctrine Module:

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

Note: this answer may be specific to these frameworks:

  • Zend Framework
  • Zend Expressive
  • Laminas
  • Mezzio

نصائح أخرى

The Hard Way

Put this into your 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);

Put this into your 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
    ]
];

Create this class: 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;
    }
}

After that, things should work splendidly

For those not using Zend Framework

I wanted to use the Doctrine console tools to validate mappings in a Symfony Bundle I was developing without having to create an entire Symfony Application just to test my work. I was able to make it work by following the Official Installation guide Here

Notes: Even though I just wanted to validate mappings, Doctrine still needed to connect to a real DB to run. Personally, using pdo_pgsql, I needed to use the host and port DBParams in addition to what the guide had.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top