草庐IT

php - 学说 :generate:crud for Entites outside a bundle

coder 2024-05-02 原文

当我尝试为实体创建原则 crud 时,我收到“未知实体 namespace 别名”异常。

我有以下项目结构

使用 src\Project\Entity 目录中的实体的一系列 bundle (在 Bundles 目录中)。

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

    namespace Project\Entity;

我觉得这可能与 auto_mapping 有关,但我已经玩了 4-5 个小时,但一无所获。

有什么建议吗?

最佳答案

解决方法:

创建一个扩展基本原则 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);
}
}

关于php - 学说 :generate:crud for Entites outside a bundle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31689469/

有关php - 学说 :generate:crud for Entites outside a bundle的更多相关文章

  1. ruby-on-rails - Prawn PDF : I need to generate nested tables - 2

    我需要一个表,其中行实际上是2行表,一个嵌套表是..我怎样才能在Prawn中做到这一点?也许我需要延期..但哪一个? 最佳答案 现在支持子表:Prawn::Document.generate("subtable.pdf")do|pdf|subtable=pdf.make_table([["sub"],["table"]])pdf.table([[subtable,"original"]])end 关于ruby-on-rails-PrawnPDF:Ineedtogeneratenested

  2. ruby-on-rails - 禁用设备的 :confirmable on-the-fly to batch-generate users - 2

    Devise是一个Ruby库,它为我提供了这个User类:classUser当写入:confirmable时,注册时会发送一封确认邮件。上周我不得不批量创建300个用户,所以我在恢复之前注释掉了:confirmable几分钟。现在我正在为用户批量创建创建一个UI,因此我需要即时添加/删除:confirmable。(我也可以直接修改Devise的源码,但我宁愿不去调和它)问题:如何即时添加/删除:confirmable? 最佳答案 WayneConrad的解决方案:user=User.newuser.skip_confirmation

  3. ruby-on-rails - 使用 RSpec 测试 CSV.generate - 2

    我在Rails3.1项目中有以下助手-我只是想知道是否有办法测试CSV.generate调用。我很想说我知道如何去做,但事实是我什至不知道从哪里开始。任何想法表示赞赏。require'csv'moduleAdmin::PurchasesHelperdefcsv_purchase_listcolumns=['course','amount','first_name','last_name','contact_phone','contact_mobile','created_at']CSV.generate(:col_sep=>";",:row_sep=>"\r\n",:headers=>

  4. ruby-on-rails - 工厂女孩/Rails : Generator to create a factory for existing model? - 2

    我在我的Rails项目中使用rspec_rails和factory_girl_railsgem。所有模型都已创建。是否有我可以运行的生成器来为现有模型创建工厂文件?例如:我已经有了一个Blog模型。RSpec允许我通过简单地运行以下命令在spec/models/blog_spec.rb生成一个模型规范文件:railsgeneraterspec:modelblog是否有我可以在命令行中运行的生成器,它会为这个现有模型生成工厂文件,位于:spec/factories/blogs.rb?我在factory_girl_rails中没有看到任何关于发电机的提及文档。

  5. ruby-on-rails - "rails generate rspec:install"似乎失败了 - 2

    运行:ruby1.9.3p0和Rails3.2.1尝试使用rspec但当我尝试将其安装到我的应用程序中时出现以下错误:/Users/Si/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/railtie/configuration.rb:85:in`method_missing':undefinedmethod`generators'for#(NoMethodError)from/Users/Si/.rvm/gems/ruby-1.9.3-p0/gems/rspec-rails-2.0.0.beta.18/lib/rspec-r

  6. ruby-on-rails - 撤消 "rails generate scaffold"后是否需要撤消 "db:migrate"? - 2

    我是RoR的新手,我正在学习MichaelHartl的教程(所以请随意更正我在您认为合适的地方使用的术语)。在第2章中,我通过运行以下行创建了一个Users表:$railsgeneratescaffoldUsername:stringemail:string$bundleexecrakedb:migrate然后,我运行了下面的代码来尝试创建一个Microposts表(但是,我拼错了没有“r”的Micropost!)...$railsgeneratescaffoldMiropostcontent:stringuser_id:integer$bundleexecrakedb:migrate

  7. ruby-on-rails - Rails sitemap_generator 未初始化常量? - 2

    我正在尝试使用Rails站点map_generatorgem为一个包含8,000,00个页面的站点生成站点地图。gem可以在这里找到:https://github.com/kjvarga/sitemap_generator这是我在sitemap.rb中的代码:require'rubygems'require'sitemap_generator'#SetthehostnameforURLcreationSitemapGenerator::Sitemap.default_host="http://www.mysite.com"SitemapGenerator::Sitemap.create

  8. ruby-on-rails - 这个 C 和 PHP 程序员如何学习 Ruby 和 Rails? - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。我来自C、php和bash背景,很容易学习,因为它们都有相同的C结构,我可以将其与我已经知道的联系起来。然后2年前我学了Python并且学得很好,Python对我来说比Ruby更容易学。然后从去年开始,我一直在尝试学习Ruby,然后是Rails,我承认,直到现在我还是学不会,讽刺的是那些打着简单易学的烙印,但是对于我这样一个老练的程序员来说,我只是无法将它

  9. ruby-on-rails - "Could not find generator figaro:install" - 2

    我正在使用来自learn-rails.com的“学习RubyOnRails”pdf书。我在第13章“配置”。我们应该在哪里执行命令:“railsgeneratefigaro:install”在第12章中,我们安装了figarogem:“我们已经在Gemfile中安装了figarogem并运行bundleinstall。”当我运行该命令时,我得到:“找不到生成器figaro:install。”我开始搜索类似的问题,我确实在这里找到了这个问题:railsgenerate-"Couldnotfindgenerator"他们被建议在命令中也包含“迁移”。我将它包含在我的命令中并让它做某事,但我

  10. ruby-on-rails - 用 Prawn 生成 PDF - 如何访问 Prawn.generate 中的变量? - 2

    我正在尝试使用Prawn生成pdf@buyer=Buyer.lastPrawn::Document.generate("samle.pdf")dotext"hello#{@buyer.name}world"end但这显然不起作用(仅当我使用类变量@@buyer时),我的问题是将变量传递给Prawn::Document.generate的正确方法是什么(我知道这个问题的解决方案是prawnto,但我正在尝试一些......而且它也是一个sinatra项目) 最佳答案 来自http://rdoc.info/github/sandal/p

随机推荐