Doctrine CLI in Slim Framework 4

Asked by At

I'm creating my first project with Slim Framework 4. I try to configure Doctrine CLI for generating entities from the database.

I have configured two files cli-config.php and boostrap.php and configured the database configuration in settings.php.

bootstrap.php looks like:

  <?php

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Cache\FilesystemCache;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\Tools\Setup;
use Psr\Container\ContainerInterface;

require_once __DIR__ . '/vendor/autoload.php';

return function (App $app) {

    $container = $app->getContainer();

    $container[EntityManager::class] = function (ContainerInterface $container): EntityManager {
        $config = Setup::createAnnotationMetadataConfiguration(
            $container['settings']['doctrine']['metadata_dirs'],
            $container['settings']['doctrine']['dev_mode']
        );

        $config->setMetadataDriverImpl(
            new AnnotationDriver(
                new AnnotationReader,
                $container['settings']['doctrine']['metadata_dirs']
            )
        );

        $config->setMetadataCacheImpl(
            new FilesystemCache(
                $container['settings']['doctrine']['cache_dir']
            )
        );

        return EntityManager::create(
            $container['settings']['doctrine']['connection'],
            $config
        );
    };

};

cli-config.php looks like:

<?php

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Console\ConsoleRunner;
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
use Symfony\Component\Console\Helper\HelperSet;
use Slim\App;

require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/bootstrap.php';

return function (App $app) {

/** @var Container $container */
//$container = require_once __DIR__ . '/bootstrap.php';
$container = $app->getContainer();

$em = $container[EntityManager::class];

$helperSet = new HelperSet(array(
    'db' => new ConnectionHelper($em->getConnection()),
    'em' => new EntityManagerHelper($em)
));

ConsoleRunner::run(

    ConsoleRunner::createHelperSet($helperSet)
);

};

When I run command php vendor/bin/doctrine in my root directory of my application I got folowing error:

PHP Fatal error:  Uncaught TypeError: Argument 1 passed to Doctrine\ORM\Tools\Console\ConsoleRunner::run() 
must be an instance of Symfony\Component\Console\Helper\HelperSet, instance of Closure given, 
called in vendor/doctrine/orm/bin/doctrine.php on line 69 and defined in vendor/doctrine/orm/lib/Doctrine/ORM/Tools/Console/ConsoleRunner.php:59
Stack trace:
#0 vendor/doctrine/orm/bin/doctrine.php(69): Doctrine\ORM\Tools\Console\ConsoleRunner::run(Object(Closure), Array)
#1 vendor/doctrine/orm/bin/doctrine(4): include('/media/Data/Pro...')
#2 {main}
  thrown in vendor/doctrine/orm/lib/Doctrine/ORM/Tools/Console/ConsoleRunner.php on line 59

Can someone help me to fix this?

0 Answers