Symfony2控制台HelperSet

时间:2012-11-20 12:29:17

标签: php symfony console doctrine-orm

我正在使用Symfony2组件创建应用程序,我陷入了symfony控制台。问题是我初始化控制台

$objectRepository = ObjectRepository::getInstance();

$console = $objectRepository->get('console');
if ( ! $console instanceof \Symfony\Component\Console\Application) {
    echo 'Failed to initialize console.' . PHP_EOL;
}

$helperSet = $console->getHelperSet();
$helperSet->set(new EntityManagerHelper($objectRepository->get('entity_manager')), 'em');

$console->run();

我有doctrine创建命令别名

namespace My\Console\Command;

use Doctrine\ORM\Tools\Console\Command\SchemaTool\CreateCommand as BaseCommand;

class CreateCommand extends BaseCommand
{
    protected function configure()
    {
        parent::configure();

        $this->setName('doctrine:schema:update');
    }

}

Doctrine \ ORM \ Tools \ Console \ Command \ SchemaTool \ CreateCommand正在使用em帮助器,问题出在Symfony\Component\Console\Application doRun() method

$command = $this->find($name);
$this->runningCommand = $command;
$statusCode = $command->run($input, $output);

应用程序在HelperSet中保留3个帮助程序,它们是(dialog,format,entityManager和em(em是entityManager的别名))。找到命令后,命令不会继承Application帮助程序集,并且只有默认对话框和格式帮助程序。

我有扩展symfony默认Application类和重写doRun()方法的解决方案,但这不是最好的方法。

1 个答案:

答案 0 :(得分:0)

看起来应用程序和命令可以有不同的帮助程序集,所以我用

解决了问题
namespace My\Console\Command;

use Doctrine\ORM\Tools\Console\Command\SchemaTool\CreateCommand as BaseCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class CreateCommand extends BaseCommand
{

    protected function configure()
    {
        parent::configure();

        $this->setName('doctrine:schema:create');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->setHelperSet($this->getApplication()->getHelperSet());

        parent::execute($input, $output);
    }

}
相关问题