/** * @inheritdoc */ public function run() { $printer = $this->getPrinter(); $arguments = $this->getArguments(); $from = $arguments['from']; $dest = realpath($arguments['dest']); $entityGenerator = new EntityGenerator(); $entityGenerator->setGenerateAnnotations(false); $entityGenerator->setGenerateStubMethods(true); $entityGenerator->setRegenerateEntityIfExists(false); $entityGenerator->setUpdateEntityIfExists(true); if (isset($arguments['extend']) && $arguments['extend']) { $entityGenerator->setClassToExtend($arguments['extend']); } if (isset($arguments['num-spaces']) && $arguments['extend']) { $entityGenerator->setNumSpaces($arguments['num-spaces']); } $reader = new ClassMetadataReader(); $reader->setEntityManager($this->getConfiguration()->getAttribute('em')); $reader->addMappingSource($from); $metadatas = $reader->getMetadatas(); foreach ($metadatas as $metadata) { $printer->writeln(sprintf('Processing entity "%s"', $printer->format($metadata->name, 'KEYWORD'))); } $entityGenerator->generate($metadatas, $dest); $printer->write(PHP_EOL); $printer->writeln(sprintf('Entity classes generated to "%s"', $printer->format($dest, 'KEYWORD'))); }
function testCreateSchema() { /* @var $em \Doctrine\ORM\EntityManager */ $em = $this->app["orm.em"]; $tool = new SchemaTool($em); //@note @doctrine générer les fichiers de classe à partir de métadonnées /* generate entity classes */ $dmf = new DisconnectedClassMetadataFactory(); $dmf->setEntityManager($em); $metadatas = $dmf->getAllMetadata(); //print_r($metadatas); $generator = new EntityGenerator(); $generator->setGenerateAnnotations(TRUE); $generator->setGenerateStubMethods(TRUE); $generator->setRegenerateEntityIfExists(TRUE); $generator->setUpdateEntityIfExists(TRUE); $generator->generate($metadatas, ROOT_TEST_DIR); $generator->setNumSpaces(4); $this->assertFileExists(ROOT_TEST_DIR . "/Entity/Post.php"); /* @note @doctrine générer la base de donnée à partir des métadonnées */ /* @see Doctrine\ORM\Tools\Console\Command\SchemaTool\CreateCommand */ /* generate database */ $tool->dropSchema($metadatas); $tool->createSchema($metadatas); $post = new \Entity\Post(); $post->setTitle("the title"); $em->persist($post); $em->flush(); $this->assertInternalType("int", $post->getId()); }
/** * @see Console\Command\Command */ protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) { $em = $this->getHelper('em')->getEntityManager(); if (\Zend_Registry::isRegistered(\LoSo_Zend_Application_Bootstrap_SymfonyContainerBootstrap::getRegistryIndex()) && ($container = \Zend_Registry::get(\LoSo_Zend_Application_Bootstrap_SymfonyContainerBootstrap::getRegistryIndex())) instanceof \Symfony\Component\DependencyInjection\ContainerInterface) { $mappingPaths = $container->getParameter('doctrine.orm.mapping_paths'); $entitiesPaths = $container->getParameter('doctrine.orm.entities_paths'); } else { $doctrineConfig = \Zend_Registry::get('doctrine.config'); $mappingPaths = $doctrineConfig['doctrine.orm.mapping_paths']; $entitiesPaths = $doctrineConfig['doctrine.orm.entities_paths']; } $cmf = new DisconnectedClassMetadataFactory($em); $metadatas = $cmf->getAllMetadata(); foreach ($mappingPaths as $namespace => $mappingPath) { // Process destination directory $destPath = realpath($entitiesPaths[$namespace]); if (!file_exists($destPath)) { throw new \InvalidArgumentException(sprintf("Entities destination directory '<info>%s</info>' does not exist.", $destPath)); } else { if (!is_writable($destPath)) { throw new \InvalidArgumentException(sprintf("Entities destination directory '<info>%s</info>' does not have write permissions.", $destPath)); } } $moduleMetadatas = MetadataFilter::filter($metadatas, $namespace); if (count($moduleMetadatas)) { // Create EntityGenerator $entityGenerator = new EntityGenerator(); $entityGenerator->setGenerateAnnotations($input->getOption('generate-annotations')); $entityGenerator->setGenerateStubMethods($input->getOption('generate-methods')); $entityGenerator->setRegenerateEntityIfExists($input->getOption('regenerate-entities')); $entityGenerator->setUpdateEntityIfExists($input->getOption('update-entities')); $entityGenerator->setNumSpaces($input->getOption('num-spaces')); if (($extend = $input->getOption('extend')) !== null) { $entityGenerator->setClassToExtend($extend); } foreach ($moduleMetadatas as $metadata) { $output->write(sprintf('Processing entity "<info>%s</info>"', $metadata->name) . PHP_EOL); } // Generating Entities $entityGenerator->generate($moduleMetadatas, $destPath); $this->_processNamespaces($destPath, $namespace); // Outputting information message $output->write(sprintf('Entity classes generated to "<info>%s</INFO>"', $destPath) . PHP_EOL); } else { $output->write('No Metadata Classes to process.' . PHP_EOL); } } /*$output->write(PHP_EOL . 'Reset database.' . PHP_EOL); $metadatas = $em->getMetadataFactory()->getAllMetadata(); $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($em); $output->write('Dropping database schema...' . PHP_EOL); $schemaTool->dropSchema($metadatas); $output->write('Database schema dropped successfully!' . PHP_EOL); $output->write('Creating database schema...' . PHP_EOL); $schemaTool->createSchema($metadatas); $output->write('Database schema created successfully!' . PHP_EOL);*/ }
protected function getEntityGenerator() { $entityGenerator = new EntityGenerator(); $entityGenerator->setGenerateAnnotations(false); $entityGenerator->setGenerateStubMethods(true); $entityGenerator->setRegenerateEntityIfExists(false); $entityGenerator->setUpdateEntityIfExists(true); $entityGenerator->setNumSpaces(4); return $entityGenerator; }
/** * Get entity generator. * * @return EntityGenerator */ protected static function getEntityGenerator() { $entityGenerator = new EntityGenerator(); $entityGenerator->setGenerateAnnotations(true); $entityGenerator->setGenerateStubMethods(true); $entityGenerator->setRegenerateEntityIfExists(true); $entityGenerator->setUpdateEntityIfExists(true); $entityGenerator->setNumSpaces(4); $entityGenerator->setAnnotationPrefix(self::$annotationPrefix); return $entityGenerator; }
protected function getEntityGenerator() { $entityGenerator = new EntityGenerator(); if (version_compare(DoctrineVersion::VERSION, "2.0.2-DEV") >= 0) { $entityGenerator->setAnnotationPrefix("orm:"); } $entityGenerator->setGenerateAnnotations(false); $entityGenerator->setGenerateStubMethods(true); $entityGenerator->setRegenerateEntityIfExists(false); $entityGenerator->setUpdateEntityIfExists(true); $entityGenerator->setNumSpaces(4); return $entityGenerator; }
public function fire() { $this->info('Starting entities generation....'); // flush all generated and cached entities, etc \D2Cache::flushAll(); $cmf = new DisconnectedClassMetadataFactory(); $cmf->setEntityManager($this->d2em); $metadata = $cmf->getAllMetadata(); if (empty($metadata)) { $this->error('No metadata found to generate entities.'); return -1; } $directory = Config::get('d2doctrine.paths.entities'); if (!$directory) { $this->error('The entity directory has not been set.'); return -1; } $entityGenerator = new EntityGenerator(); $entityGenerator->setGenerateAnnotations($this->option('generate-annotations')); $entityGenerator->setGenerateStubMethods($this->option('generate-methods')); $entityGenerator->setRegenerateEntityIfExists($this->option('regenerate-entities')); $entityGenerator->setUpdateEntityIfExists($this->option('update-entities')); $entityGenerator->setNumSpaces($this->option('num-spaces')); $entityGenerator->setBackupExisting(!$this->option('no-backup')); $this->info('Processing entities:'); foreach ($metadata as $item) { $this->line($item->name); } try { $entityGenerator->generate($metadata, $directory); $this->info('Entities have been created.'); } catch (\ErrorException $e) { if ($this->option('verbose') == 3) { throw $e; } $this->error("Caught ErrorException: " . $e->getMessage()); $this->info("Re-optimizing:"); $this->call('optimize'); $this->comment("*** You must now rerun this artisan command ***"); exit(-1); } }
/** * @see Console\Command\Command */ protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) { $em = $this->getHelper('em')->getEntityManager(); $cmf = new DisconnectedClassMetadataFactory(); $cmf->setEntityManager($em); $metadatas = $cmf->getAllMetadata(); $metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter')); // Process destination directory $destPath = realpath($input->getArgument('dest-path')); if (!file_exists($destPath)) { throw new \InvalidArgumentException(sprintf("Entities destination directory '<info>%s</info>' does not exist.", $destPath)); } else { if (!is_writable($destPath)) { throw new \InvalidArgumentException(sprintf("Entities destination directory '<info>%s</info>' does not have write permissions.", $destPath)); } } if (count($metadatas)) { // Create EntityGenerator $entityGenerator = new EntityGenerator(); $entityGenerator->setGenerateAnnotations($input->getOption('generate-annotations')); $entityGenerator->setGenerateStubMethods($input->getOption('generate-methods')); $entityGenerator->setRegenerateEntityIfExists($input->getOption('regenerate-entities')); $entityGenerator->setUpdateEntityIfExists($input->getOption('update-entities')); $entityGenerator->setNumSpaces($input->getOption('num-spaces')); if (($extend = $input->getOption('extend')) !== null) { $entityGenerator->setClassToExtend($extend); } foreach ($metadatas as $metadata) { $output->write(sprintf('Processing entity "<info>%s</info>"', $metadata->name) . PHP_EOL); } // Generating Entities $entityGenerator->generate($metadatas, $destPath); // Outputting information message $output->write(PHP_EOL . sprintf('Entity classes generated to "<info>%s</INFO>"', $destPath) . PHP_EOL); } else { $output->write('No Metadata Classes to process.' . PHP_EOL); } }
/** * Get a Doctrine EntityGenerator instance. * * @param string|null $classToExtend * * @return EntityGenerator */ protected function getEntityGenerator($classToExtend = null) { $entityGenerator = new EntityGenerator(); if (!is_null($classToExtend)) { $entityGenerator->setClassToExtend($classToExtend); } $entityGenerator->setGenerateAnnotations(true); $entityGenerator->setGenerateStubMethods(true); $entityGenerator->setRegenerateEntityIfExists(false); $entityGenerator->setUpdateEntityIfExists(true); $entityGenerator->setNumSpaces(4); $entityGenerator->setAnnotationPrefix('ORM\\'); return $entityGenerator; }
/** * @return \Doctrine\ORM\Tools\EntityGenerator */ protected function getEntityGenerator() { $entityGenerator = new EntityGenerator(); $entityGenerator->setClassToExtend('Kunstmaan\\AdminBundle\\Entity\\AbstractEntity'); $entityGenerator->setGenerateAnnotations(true); $entityGenerator->setGenerateStubMethods(true); $entityGenerator->setRegenerateEntityIfExists(false); $entityGenerator->setUpdateEntityIfExists(true); $entityGenerator->setNumSpaces(4); $entityGenerator->setAnnotationPrefix('ORM\\'); return $entityGenerator; }
/** * main method * * @return void */ public function main() { static $em; if ($em === null) { $wd = getcwd(); $zf = $this->project->getProperty('zf'); $application = (require $zf); if (!$application instanceof Application) { throw new BuildException(sprintf('zf bootstrap file "%s" should return an instance of Zend\\Mvc\\Application', $zf)); } chdir($wd); $em = $application->getServiceManager()->get($this->em); } $cmf = new DisconnectedClassMetadataFactory(); $cmf->setEntityManager($em); $metadatas = $cmf->getAllMetadata(); if (!empty($this->filter)) { $metadatas = MetadataFilter::filter($metadatas, $this->filter); } if (count($metadatas)) { // Create EntityGenerator $entityGenerator = new EntityGenerator(); $entityGenerator->setGenerateAnnotations(true); $entityGenerator->setGenerateStubMethods(true); $entityGenerator->setRegenerateEntityIfExists(true); $entityGenerator->setUpdateEntityIfExists(true); $entityGenerator->setNumSpaces(4); foreach ($metadatas as $metadata) { $this->log(sprintf('Processing entity %s', $metadata->name)); } // Generating Entities $entityGenerator->generate($metadatas, $this->output); // Outputting information message $this->log(sprintf('Entity classes generated to %s', $this->output)); } else { $this->log('No metadata classes to process'); } }