doctrine:generate:crud for Entites outside a bundle doctrine:generate:crud for Entites outside a bundle symfony symfony

doctrine:generate:crud for Entites outside a bundle


Solution:

Create a command which extends the base doctrine crud command

extends \Sensio\Bundle\GeneratorBundle\Command\GenerateDoctrineCrudCommand

Modifying

 $entityClass = $this->getContainer()->get('doctrine')->getAliasNamespace('Project').'\\'.$entity;

to the namespace of the entity. By default it assumes the entity is in the Bundle where you want the CRUD to be created.

By setting

$this->setName('project:generate:crud');

in the Configre() function you can call the function from your command line

Example:

<?phpnamespace 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);}}


The problem is that you are keeping your entities outside bundles. Since this is not standard behaviour, you have to extend or create another GenerateDoctrineCrudCommand to be able to pass namespace alias.