protected function execute(InputInterface $input, OutputInterface $output)
 {
     $manager = new MetadataFactory($this->container->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->container->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();
     $generator->setBackupExisting(!$input->getOption('no-backup'));
     $repoGenerator = new EntityRepositoryGenerator();
     foreach ($metadata->getMetadata() as $m) {
         $output->writeln(sprintf('  > generating <comment>%s</comment>', $m->name));
         $generator->generate(array($m), $metadata->getPath());
         if ($m->customRepositoryClassName && false !== strpos($m->customRepositoryClassName, $metadata->getNamespace())) {
             $repoGenerator->writeEntityRepositoryClass($m->customRepositoryClassName, $metadata->getPath());
         }
     }
 }
예제 #2
0
 public function testNewCg07()
 {
     $utils = new Utils();
     $factory = new MetadataFactory($this->doctrine);
     $entityInfo = $factory->getClassMetadata('Lowbi\\SystemBundle\\Entity\\Cg07')->getMetadata();
     $simpleField = array();
     $entityField = array();
     foreach ($entityInfo[0]->fieldMappings as $field) {
         if ($field['fieldName'] != 'id') {
             $simpleField[] = $field;
         }
     }
     foreach ($entityInfo[0]->associationMappings as $field) {
         $entityField[] = $field;
     }
     $myEntity = new Category();
     foreach ($simpleField as $field) {
         $setter = 'set' . ucfirst($field['fieldName']);
         if (method_exists($myEntity, $setter)) {
             $myEntity->{$setter}($utils->fillField($field));
         }
     }
     // @codeCoverageIgnoreStart
     foreach ($entityField as $entity) {
         switch ($entity['type']) {
             case ClassMetadataInfo::MANY_TO_MANY:
                 $itemTolink = $this->em->getRepository($entity['targetEntity'])->findAll();
                 $fieldName = substr($entity['fieldName'], 0, strlen($entity['fieldName']) - 1);
                 $setter = 'add' . ucfirst($fieldName);
                 if (method_exists($myEntity, $setter) && count($itemTolink) > 0) {
                     $myEntity->{$setter}($itemTolink[0]);
                 }
                 break;
             case ClassMetadataInfo::MANY_TO_ONE:
                 $itemTolink = $this->em->getRepository($entity['targetEntity'])->findAll();
                 $fieldName = substr($entity['fieldName'], 0, strlen($entity['fieldName']) - 1);
                 $setter = 'set' . ucfirst($fieldName);
                 if (method_exists($myEntity, $setter) && count($itemTolink) > 0) {
                     $myEntity->{$setter}($itemTolink[0]);
                 }
                 break;
             case ClassMetadataInfo::ONE_TO_ONE:
                 $itemTolink = $this->em->getRepository($entity['targetEntity'])->findAll();
                 $fieldName = substr($entity['fieldName'], 0, strlen($entity['fieldName']) - 1);
                 $setter = 'set' . ucfirst($fieldName);
                 if (method_exists($myEntity, $setter) && count($itemTolink) > 0) {
                     $myEntity->{$setter}($itemTolink[0]);
                 }
                 break;
         }
     }
     // @codeCoverageIgnoreEnd
     $this->em->persist($myEntity);
     $this->em->flush();
     return $entityField;
 }
 protected function getEntityMetadata($entity)
 {
     $factory = new MetadataFactory($this->getContainer()->get('doctrine'));
     return $factory->getClassMetadata($entity)->getMetadata();
 }
예제 #4
0
파일: setUp.php 프로젝트: r4cker/lowbi
 private function loadEntityData()
 {
     $factory = new MetadataFactory($this->doctrine);
     $handle = opendir($this->baseDir . '\\..\\src\\Lowbi\\SystemBundle\\Entity\\');
     $urlArray = array();
     $entitiesToSkip = array('User', 'Role');
     $entityField = array();
     while (false !== ($entry = readdir($handle))) {
         if ($entry != '.' && $entry != '..') {
             $entityName = substr($entry, 0, -4);
             if (!in_array($entityName, $entitiesToSkip)) {
                 $entityClassName = "Lowbi\\SystemBundle\\Entity\\" . $entityName;
                 $entityInfo = $factory->getClassMetadata($entityClassName)->getMetadata();
                 $itr = 0;
                 foreach ($entityInfo[0]->fieldMappings as $field) {
                     if ($itr < 4 && $field['fieldName'] != 'id') {
                         $entityField[$entityName][] = $field;
                     } else {
                         if ($field['fieldName'] != 'id') {
                             if (array_key_exists('nullable', $field)) {
                                 if ($field['nullable'] == true) {
                                     $entityField[$entityName][] = $field;
                                 }
                             }
                             if (array_key_exists('required', $field)) {
                                 if ($field['required'] == true) {
                                     $entityField[$entityName][] = $field;
                                 }
                             }
                         }
                     }
                     $itr++;
                 }
             }
         }
     }
     foreach ($entityField as $key => $entity) {
         $myEntityName = 'Lowbi\\SystemBundle\\Entity\\' . ucfirst($key);
         $myEntity = new $myEntityName();
         foreach ($entity as $field) {
             $setter = 'set' . ucfirst($field['fieldName']);
             $myEntity->{$setter}($this->fillField($field));
         }
         $this->em->persist($myEntity);
         $this->em->flush();
     }
 }