protected function execute(InputInterface $input, OutputInterface $output)
 {
     $manager = new DisconnectedMetadataFactory($this->getContainer()->get('doctrine'));
     try {
         $bundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('name'));
         $output->writeln(sprintf('Clearing entities for bundle "<info>%s</info>"', $bundle->getName()));
         $metadata = $manager->getBundleMetadata($bundle);
     } catch (\InvalidArgumentException $e) {
         $name = strtr($input->getArgument('name'), '/', '\\');
         if (false !== ($pos = strpos($name, ':'))) {
             $name = $this->getContainer()->get('doctrine')->getAliasNamespace(substr($name, 0, $pos)) . '\\' . substr($name, $pos + 1);
         }
         if (class_exists($name)) {
             $output->writeln(sprintf('Fixing entity "<info>%s</info>"', $name));
             $metadata = $manager->getClassMetadata($name, $input->getOption('path'));
         } else {
             $output->writeln(sprintf('Fixing entities for namespace "<info>%s</info>"', $name));
             $metadata = $manager->getNamespaceMetadata($name, $input->getOption('path'));
         }
     }
     foreach ($metadata->getMetadata() as $m) {
         // Getting the metadata for the entity class once more to get the correct path if the namespace has multiple occurrences
         try {
             $entityMetadata = $manager->getClassMetadata($m->getName(), $input->getOption('path'));
         } catch (\RuntimeException $e) {
             // fall back to the bundle metadata when no entity class could be found
             $entityMetadata = $metadata;
         }
         $output->writeln(sprintf('  > fixing <comment>%s</comment>', $m->name));
         $res = $this->fixEntity($m, $entityMetadata->getPath());
         if (!$res) {
             $output->writeln(sprintf('> FAILED'));
         }
     }
 }
Example #2
0
 protected function createForOrm(BundleInterface $bundle, $entities, CrudGenerator $generator, OutputInterface $output)
 {
     $manager = new DisconnectedMetadataFactory($this->getContainer()->get('doctrine'));
     $metadata = $manager->getBundleMetadata($bundle);
     $generator->openFile();
     /** @var ClassMetadata $m */
     foreach ($metadata->getMetadata() as $m) {
         try {
             $entityMetadata = $manager->getClassMetadata($m->getName());
         } catch (\RuntimeException $e) {
             $entityMetadata = $metadata;
         }
         $name = str_replace($entityMetadata->getNamespace() . '\\', '', $m->name);
         if (0 < count($entities) && !in_array($name, $entities)) {
             continue;
         }
         $output->writeln(sprintf('  > generating <comment>%s</comment>', $m->name));
         $properties = [];
         /** @var  $column */
         foreach ($m->getReflectionProperties() as $key => $column) {
             if ($key !== 'id') {
                 $properties[] = $key;
             }
         }
         $generator->generate($name, $entityMetadata->getNamespace(), $properties);
         $generator->register($name, $entityMetadata->getNamespace());
     }
     $generator->closeFile();
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $manager = new DisconnectedMetadataFactory($this->getContainer()->get('doctrine'));
     try {
         $bundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('name'));
         $output->writeln(sprintf('Generating entities for bundle "<info>%s</info>"', $bundle->getName()));
         $metadata = $manager->getBundleMetadata($bundle);
     } catch (\InvalidArgumentException $e) {
         $name = strtr($input->getArgument('name'), '/', '\\');
         if (false !== ($pos = strpos($name, ':'))) {
             $name = $this->getContainer()->get('doctrine')->getEntityNamespace(substr($name, 0, $pos)) . '\\' . substr($name, $pos + 1);
         }
         if (class_exists($name)) {
             $output->writeln(sprintf('Generating entity "<info>%s</info>"', $name));
             $metadata = $manager->getClassMetadata($name, $input->getOption('path'));
         } else {
             $output->writeln(sprintf('Generating entities for namespace "<info>%s</info>"', $name));
             $metadata = $manager->getNamespaceMetadata($name, $input->getOption('path'));
         }
     }
     $generator = $this->getEntityGenerator();
     $backupExisting = !$input->getOption('no-backup');
     $generator->setBackupExisting($backupExisting);
     $repoGenerator = new EntityRepositoryGenerator();
     foreach ($metadata->getMetadata() as $m) {
         if ($backupExisting) {
             $basename = substr($m->name, strrpos($m->name, '\\') + 1);
             $output->writeln(sprintf('  > backing up <comment>%s.php</comment> to <comment>%s.php~</comment>', $basename, $basename));
         }
         // Getting the metadata for the entity class once more to get the correct path if the namespace has multiple occurrences
         try {
             $entityMetadata = $manager->getClassMetadata($m->getName(), $input->getOption('path'));
         } catch (\RuntimeException $e) {
             // fall back to the bundle metadata when no entity class could be found
             $entityMetadata = $metadata;
         }
         $output->writeln(sprintf('  > generating <comment>%s</comment>', $m->name));
         $generator->generate(array($m), $entityMetadata->getPath());
         if ($m->customRepositoryClassName && false !== strpos($m->customRepositoryClassName, $metadata->getNamespace())) {
             $repoGenerator->writeEntityRepositoryClass($m->customRepositoryClassName, $metadata->getPath());
         }
     }
 }
 protected function getEntityMetadata($entity)
 {
     $factory = new DisconnectedMetadataFactory($this->getContainer()->get('doctrine'));
     return $factory->getClassMetadata($entity)->getMetadata();
 }
 /**
  * Generates the show.html.twig template in the final bundle.
  *
  * @param string $dir The path to the folder that hosts templates in the bundle
  */
 private function generateShowView($dir)
 {
     //-------- CPANA -----------------------------------------
     $entity_assoc = NULL;
     // is the name of the property holding objects from the associatied class ( and not the name of the actual assoc. class)
     $fields_assoc = NULL;
     $entity_assoc_class = NULL;
     //the name of the associated class
     $keys = array();
     //--- verify if there is any property representing an association (one-to-many bidirectional etc)
     if (!empty(array_keys($this->metadata->associationMappings))) {
         $assoc = $this->metadata->associationMappings;
         foreach ($assoc as $key => $value) {
             if ($assoc[$key]['isOwningSide'] == 0) {
                 $keys[] = $assoc[$key]['fieldName'];
                 $entity_assoc_class = $assoc[$key]['targetEntity'];
             }
         }
         if (!empty($keys)) {
             //it is possible that the entity has association but if are owning sides we do not want to add them, so is possible that the
             $entity_assoc = $keys[0];
             // array keys[] to be empty
             global $kernel;
             //$entityClass = 'AppBundle\\Entity\\'.$entity_assoc;
             $factory = new DisconnectedMetadataFactory($kernel->getContainer()->get('doctrine'));
             $metadata_assoc = $factory->getClassMetadata($entity_assoc_class)->getMetadata();
             $fields_assoc = $metadata_assoc[0]->fieldMappings;
         }
     }
     //---------------------------------------
     $this->renderFile('crud/views/show.html.twig.twig', $dir . '/show.html.twig', array('bundle' => $this->bundle->getName(), 'entity' => $this->entity, 'fields' => $this->metadata->fieldMappings, 'entity_assoc' => $entity_assoc, 'fields_assoc' => $fields_assoc, 'entity_assoc_class' => $entity_assoc_class, 'actions' => $this->actions, 'route_prefix' => $this->routePrefix, 'route_name_prefix' => $this->routeNamePrefix, 'layout' => $this->layout, 'bodyBlock' => $this->bodyBlock));
 }
 protected function getClassPath($entityName)
 {
     $manager = new DisconnectedMetadataFactory($this->getContainer()->get('doctrine'));
     $classPath = $manager->getClassMetadata($entityName)->getPath();
     return $classPath;
 }
 /**
  * Sets the variables holding information about associated entities in bidirectional relations.
  *
  * @author Cristian Pana
  */
 protected function getAssociatedEntities()
 {
     $keys = array();
     //--- verify if there is any property representing an association (one-to-many bidirectional etc)
     if (!empty(array_keys($this->metadata->associationMappings))) {
         $assoc = $this->metadata->associationMappings;
         foreach ($assoc as $key => $value) {
             if (0 == $assoc[$key]['isOwningSide']) {
                 $keys[] = $assoc[$key]['fieldName'];
                 $this->entityAssocClassName[$assoc[$key]['fieldName']] = $assoc[$key]['targetEntity'];
             }
         }
         if (!empty($keys)) {
             //it is possible that the entity has association but if they are owning sides we do not want to add them,
             $this->entityAssocProperty = (array) $keys;
             global $kernel;
             $factory = new DisconnectedMetadataFactory($kernel->getContainer()->get('doctrine'));
             foreach ($this->entityAssocClassName as $fieldName => $className) {
                 //$metadata_assoc = $factory->getClassMetadata($this->entityAssocClassName[$value])->getMetadata();
                 $metadata_assoc = $factory->getClassMetadata($className)->getMetadata();
                 $this->fieldsOfAssocEntity[$fieldName] = $metadata_assoc[0]->fieldMappings;
             }
         }
     }
 }
 /**
  * Returning ClassMetadata
  *
  * @param InputInterface $input An InputInterface instance
  * @param OutputInterface $output An OutputInterface instance
  * @param bool $displayStatus Displaying process messages
  *
  * @return ClassMetadataCollection
  */
 protected function getMetadata(InputInterface $input, OutputInterface $output, $displayStatus)
 {
     $manager = new DisconnectedMetadataFactory($this->getContainer()->get('doctrine'));
     try {
         $bundle = $this->getContainer()->get("kernel")->getBundle($input->getArgument('name'));
         if ($displayStatus) {
             $output->writeln(sprintf('Generating entities for bundle "<info>%s</info>"', $bundle->getName()));
         }
         $metadata = $manager->getBundleMetadata($bundle);
     } catch (\InvalidArgumentException $e) {
         $name = strtr($input->getArgument('name'), '/', '\\');
         if (false !== ($pos = strpos($name, ':'))) {
             $name = $this->getContainer()->get('doctrine')->getAliasNamespace(substr($name, 0, $pos)) . '\\' . substr($name, $pos + 1);
         }
         if (class_exists($name)) {
             if ($displayStatus) {
                 $output->writeln(sprintf('Generating entity "<info>%s</info>"', $name));
             }
             $metadata = $manager->getClassMetadata($name, $input->getOption('path'));
         } else {
             if ($displayStatus) {
                 $output->writeln(sprintf('Generating entities for namespace "<info>%s</info>"', $name));
             }
             $metadata = $manager->getNamespaceMetadata($name, $input->getOption('path'));
         }
     }
     return $metadata;
 }