doctrine:generate:crud 对于束外的实体
doctrine:generate:crud for Entites outside a bundle
当我尝试为一个实体创建一个 doctrine crud 时,我得到一个 'Unknown entity namespace alias' 异常。
我有以下项目结构
使用 src\Project\Entity 目录中的实体的一系列捆绑包(在 Bundles 目录中)。
如您所见,我的实体命名空间是
namespace Project\Entity;
我觉得这可能与 auto_mapping 有关,但我已经玩了 4-5 个小时,但一无所获。
有什么建议吗?
问题是您将实体保留在包之外。由于这不是标准行为,您必须扩展或创建另一个 GenerateDoctrineCrudCommand
才能传递命名空间别名。
解决方案:
创建一个扩展基本学说 crud 命令的命令
延长 \Sensio\Bundle\GeneratorBundle\Command\GenerateDoctrineCrudCommand
正在修改
$entityClass = $this->getContainer()->get('doctrine')->getAliasNamespace('Project').'\'.$entity;
到实体的命名空间。默认情况下,它假定实体位于您希望创建 CRUD 的 Bundle 中。
通过设置
$this->setName('project:generate:crud');
在 Configre() 函数中,您可以从命令行调用该函数
示例:
<?php
namespace Project\Bundle\UtilityBundle\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Sensio\Bundle\GeneratorBundle\Command\Validators;
class GenerateDoctrineCrudCommand extends \Sensio\Bundle\GeneratorBundle\Command\GenerateDoctrineCrudCommand
{
protected function configure()
{
parent::configure();
$this->setName('project:generate:crud');
$this->setDescription('CRUD generator that supports entities outside a bundle');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$questionHelper = $this->getQuestionHelper();
if ($input->isInteractive()) {
$question = new ConfirmationQuestion($questionHelper->getQuestion('Do you confirm generation', 'yes', '?'), true);
if (!$questionHelper->ask($input, $output, $question)) {
$output->writeln('<error>Command aborted</error>');
return 1;
}
}
// Note: this expects an argument like InterpracCorporateFrontendBundle:Notification
list($bundle, $entity) = explode(':', $input->getOption('entity'));
$format = Validators::validateFormat($input->getOption('format'));
$prefix = $this->getRoutePrefix($input, $entity);
$withWrite = $input->getOption('with-write');
$forceOverwrite = $input->getOption('overwrite');
$questionHelper->writeSection($output, 'CRUD generation');
$entityClass = $this->getContainer()->get('doctrine')->getAliasNamespace('Project').'\'.$entity;
$metadata = $this->getEntityMetadata($entityClass);
$bundle = $this->getContainer()->get('kernel')->getBundle($bundle);
$generator = $this->getGenerator($bundle);
$generator->generate($bundle, $entity, $metadata[0], $format, $prefix, $withWrite, $forceOverwrite);
$output->writeln('Generating the CRUD code: <info>OK</info>');
$errors = array();
$runner = $questionHelper->getRunner($output, $errors);
// form
if ($withWrite) {
$output->write('Generating the Form code: ');
if ($this->generateForm($bundle, $entity, $metadata)) {
$output->writeln('<info>OK</info>');
} else {
$output->writeln('<warning>Already exists, skipping</warning>');
}
}
// routing
if ('annotation' != $format) {
$runner($this->updateRouting($questionHelper, $input, $output, $bundle, $format, $entity, $prefix));
}
$questionHelper->writeGeneratorSummary($output, $errors);
}
}
当我尝试为一个实体创建一个 doctrine crud 时,我得到一个 'Unknown entity namespace alias' 异常。
我有以下项目结构
使用 src\Project\Entity 目录中的实体的一系列捆绑包(在 Bundles 目录中)。
如您所见,我的实体命名空间是
namespace Project\Entity;
我觉得这可能与 auto_mapping 有关,但我已经玩了 4-5 个小时,但一无所获。
有什么建议吗?
问题是您将实体保留在包之外。由于这不是标准行为,您必须扩展或创建另一个 GenerateDoctrineCrudCommand
才能传递命名空间别名。
解决方案:
创建一个扩展基本学说 crud 命令的命令
延长 \Sensio\Bundle\GeneratorBundle\Command\GenerateDoctrineCrudCommand
正在修改
$entityClass = $this->getContainer()->get('doctrine')->getAliasNamespace('Project').'\'.$entity;
到实体的命名空间。默认情况下,它假定实体位于您希望创建 CRUD 的 Bundle 中。
通过设置
$this->setName('project:generate:crud');
在 Configre() 函数中,您可以从命令行调用该函数
示例:
<?php
namespace Project\Bundle\UtilityBundle\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Sensio\Bundle\GeneratorBundle\Command\Validators;
class GenerateDoctrineCrudCommand extends \Sensio\Bundle\GeneratorBundle\Command\GenerateDoctrineCrudCommand
{
protected function configure()
{
parent::configure();
$this->setName('project:generate:crud');
$this->setDescription('CRUD generator that supports entities outside a bundle');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$questionHelper = $this->getQuestionHelper();
if ($input->isInteractive()) {
$question = new ConfirmationQuestion($questionHelper->getQuestion('Do you confirm generation', 'yes', '?'), true);
if (!$questionHelper->ask($input, $output, $question)) {
$output->writeln('<error>Command aborted</error>');
return 1;
}
}
// Note: this expects an argument like InterpracCorporateFrontendBundle:Notification
list($bundle, $entity) = explode(':', $input->getOption('entity'));
$format = Validators::validateFormat($input->getOption('format'));
$prefix = $this->getRoutePrefix($input, $entity);
$withWrite = $input->getOption('with-write');
$forceOverwrite = $input->getOption('overwrite');
$questionHelper->writeSection($output, 'CRUD generation');
$entityClass = $this->getContainer()->get('doctrine')->getAliasNamespace('Project').'\'.$entity;
$metadata = $this->getEntityMetadata($entityClass);
$bundle = $this->getContainer()->get('kernel')->getBundle($bundle);
$generator = $this->getGenerator($bundle);
$generator->generate($bundle, $entity, $metadata[0], $format, $prefix, $withWrite, $forceOverwrite);
$output->writeln('Generating the CRUD code: <info>OK</info>');
$errors = array();
$runner = $questionHelper->getRunner($output, $errors);
// form
if ($withWrite) {
$output->write('Generating the Form code: ');
if ($this->generateForm($bundle, $entity, $metadata)) {
$output->writeln('<info>OK</info>');
} else {
$output->writeln('<warning>Already exists, skipping</warning>');
}
}
// routing
if ('annotation' != $format) {
$runner($this->updateRouting($questionHelper, $input, $output, $bundle, $format, $entity, $prefix));
}
$questionHelper->writeGeneratorSummary($output, $errors);
}
}