首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >原则:生成:用于包外的实体的crud

原则:生成:用于包外的实体的crud
EN

Stack Overflow用户
提问于 2015-07-29 00:46:35
回答 2查看 793关注 0票数 3

当我试图为一个实体创建一个理论crud时,我会得到一个“未知实体名称空间别名”异常。

我有以下的项目结构

使用src\Project\Entity目录中的实体使用一系列捆绑包(在bundles目录中)。

如您所见,我的实体命名空间是

代码语言:javascript
复制
    namespace Project\Entity;

我有一种感觉,这可能是与auto_mapping有关,但我已经玩了4-5个小时,没有任何进展。

有什么建议吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-07-29 03:43:12

解决方案:

创建一个扩展基本理论crud命令的命令

扩展\Sensio\Bundle\GeneratorBundle\Command\GenerateDoctrineCrudCommand

改性

代码语言:javascript
复制
 $entityClass = $this->getContainer()->get('doctrine')->getAliasNamespace('Project').'\\'.$entity;

到实体的命名空间。默认情况下,它假设实体位于要创建CRUD的包中。

通过设置

代码语言:javascript
复制
$this->setName('project:generate:crud');

在Configre()函数中,可以从命令行调用该函数

示例:

代码语言:javascript
复制
<?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);
}
}
票数 3
EN

Stack Overflow用户

发布于 2015-07-29 02:14:42

问题是你把你的实体放在捆绑包之外。因为这不是标准行为,所以必须扩展或创建另一个GenerateDoctrineCrudCommand才能传递名称空间别名。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31689469

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档