Teaching: generate: crud for Entites outside the package
When I try to create a crud doctrine for an entity, I get an "Unknown namespace alias" exception.
I have the following project structure
With a series of bundles (in the Bundles directory) using entities in the src \ Project \ Entity directory.
As you can see my object namespace is
namespace Project\Entity;
I have a feeling it might have something to do with auto_mapping, but I've been playing around with this for 4-5 hours and won't go anywhere.
Any suggestions?
source to share
Decision:
Create a command that extends the basic doctrine of the crud command
extends \ Sensio \ Bundle \ GeneratorBundle \ Command \ GenerateDoctrineCrudCommand
Change
$entityClass = $this->getContainer()->get('doctrine')->getAliasNamespace('Project').'\\'.$entity;
into the object's namespace. By default, the object is assumed to be in the Bundle where you want to create the CRUD.
By setting
$this->setName('project:generate:crud');
in Configre () function you can call the function from command line
Example:
<?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);
}
}
source to share