doctrine_cli_silex

Doctrine2 CLI under Silex application

I got it running with Doctrine 2.3.1-DEV. Get doctrine provider using composer:

{
    "minimum-stability": "dev",
    "require": {
        "silex/silex": "1.0.*",
        "taluu/doctrine-orm-provider" : "*",
    },
 
    "autoload": {
        "psr-0": { "Entity": "app/" }
    }
}

Create doctrine.php and put the following content inside. I like to put it in bin/doctrine.php:

#!/usr/bin/env php5
<?php
// Load your bootstrap or instantiate application and setup your config here
 
require_once APP_ROOT .'/vendor/autoload.php';
 
$app        = new Silex\Application();
 
// Doctrine DBAL
$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
    'db.options' => $config['db']
));
 
// Doctrine ORM, I like the defaults, so I've only modified the proxies dir and entities path / namespace
$app->register(new Nutwerk\Provider\DoctrineORMServiceProvider(), array(
    'db.orm.entities'              => array(array(
        'type'      => 'annotation',
        'path'      => APP_PATH .'/Entity',
        'namespace' => 'Entity',
    )),
    'db.orm.proxies_dir'           => APP_ROOT .'/var/cache/doctrine/Proxy',
));
 
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
use Doctrine\ORM\Tools\Console\ConsoleRunner;
use Symfony\Component\Console\Helper\HelperSet;
 
$helperSet = new HelperSet(array(
    'db' => new ConnectionHelper($app['db.orm.em']->getConnection()),
    'em' => new EntityManagerHelper($app['db.orm.em'])
));
 
ConsoleRunner::run($helperSet);

And that’s it :)

  • http://mavimo.org avimo

    Better solution can be to use:

    new ConnectionHelper($app['db.orm.em']->getConnection()),
    ‘em’ => new EntityManagerHelper($app['db.orm.em'])
    ));

    $console->setHelperSet($helperSet);
    ConsoleRunner::addCommands($console);

    // …

    $console->run();
    ?>

    This make possibile to add other command in the same console application and run it in a different moment.

  • Robert Cambridge

    Fatal error: Uncaught exception ‘InvalidArgumentException’ with message ‘Identifier “db.orm.em” is not defined.’

    I had to use $app['orm.em']

  • JamesMallison

    This was great, very helpful, even now! Thanks a lot!