gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 00:59:25 24 4
gpt4 key购买 nike

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

enter image description here

我有以下项目结构

enter image description here

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

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

    namespace Project\Entity;

enter image description here

我觉得这可能与 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com