/**
  * Creates the needed DB schema using Doctrine's SchemaTool. If tables already
  * exist, this will throw an exception.
  *
  * @param string $outputPathAndFilename A file to write SQL to, instead of executing it
  * @return string
  */
 public function createSchema($outputPathAndFilename = null)
 {
     $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->entityManager);
     if ($outputPathAndFilename === null) {
         $schemaTool->createSchema($this->entityManager->getMetadataFactory()->getAllMetadata());
     } else {
         file_put_contents($outputPathAndFilename, implode(PHP_EOL, $schemaTool->getCreateSchemaSql($this->entityManager->getMetadataFactory()->getAllMetadata())));
     }
 }
 public function create_dump_sql()
 {
     $driver = $this->em->getConfiguration()->getMetadataDriverImpl();
     $entities_names = $driver->getAllClassNames();
     $entities_metadata = array();
     foreach ($entities_names as $name) {
         array_push($entities_metadata, $this->em->getClassMetadata($name));
     }
     $tool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
     return $tool->getCreateSchemaSql($entities_metadata);
 }
 /**
  * @param Configuration $cliConfig
  */
 protected function _doRun(\Doctrine\Common\Cli\Configuration $cliConfig)
 {
     if (!is_writable(dirname($this->installSqlFile))) {
         throw new \BuildException("Directory to write Install Sql File into is not writable.");
     }
     $em = $cliConfig->getAttribute('em');
     $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($em);
     $cmf = $em->getMetadataFactory();
     $classes = $cmf->getAllMetadata();
     $sql = $schemaTool->getCreateSchemaSql($classes);
     $code = "<?php\n\nreturn " . var_export($sql, true) . ";\n";
     file_put_contents($this->installSqlFile, $code);
     $this->log("Wrote the Array of SQL statements to create schema to file " . $this->installSqlFile);
 }
 /**
  * @see Console\Command\Command
  */
 public function execute(InputInterface $input, OutputInterface $output)
 {
     $container = $this->getApplication()->getKernel()->getContainer();
     $em = $container->getService('em');
     $entityMetaClasses = array();
     foreach ($input->getArgument('entity') as $entity) {
         $entityMetaClasses[] = $em->getClassMetadata($entity);
     }
     $tool = new \Doctrine\ORM\Tools\SchemaTool($em);
     if ($input->getOption('alter')) {
         $schema = $tool->getUpdateSchemaSql($entityMetaClasses, true);
     } else {
         $schema = $tool->getCreateSchemaSql($entityMetaClasses);
     }
     array_walk($schema, function (&$value) {
         $value = $value . ';';
     });
     $output->writeln($schema);
 }
Exemple #5
0
 /**
  * Secret function, to create/update database schema from doctrine entities
  * @param type $mode
  * @return type
  */
 function db_schema($mode = "update")
 {
     try {
         $this->em = $this->doctrine->em;
         $tool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
         $cmf = new \Doctrine\ORM\Tools\DisconnectedClassMetadataFactory();
         $cmf->setEntityManager($this->em);
         $metadata = $cmf->getAllMetadata();
         if ($mode == "create") {
             $queries = $tool->getCreateSchemaSql($metadata);
         } else {
             $queries = $tool->getUpdateSchemaSql($metadata);
         }
         echo "Total queries: " . count($queries) . "<br /><br />";
         for ($i = 0; $i < count($queries); $i++) {
             $this->db->query($queries[$i]);
             echo $queries[$i] . "<br /><br />Execution Successfull: " . ($i + 1) . "<br /><br />";
         }
     } catch (Exception $err) {
         log_message("error", $err->getMessage());
         return show_error($err->getMessage());
     }
 }
Exemple #6
0
 public function createSchema()
 {
     $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
     $classes = $this->em->getMetadataFactory()->getAllMetadata();
     $sql = $schemaTool->getCreateSchemaSql($classes);
     $schemaTool->createSchema($classes, true);
     return $sql;
 }
Exemple #7
0
 /**
  * Get structures to save when module is disabled
  *
  * @param string $author Module author
  * @param string $name   Module name
  *
  * @return array
  */
 public static function getModuleProtectedStructures($author, $name)
 {
     $tables = array();
     $columns = array();
     $path = static::getAbsoluteDir($author, $name) . 'Model';
     if (\Includes\Utils\FileManager::isExists($path)) {
         $filter = new \Includes\Utils\FileFilter($path, '/.*\\.php$/Si');
         foreach ($filter->getIterator() as $path => $data) {
             // DO NOT call "getInterfaces()" after the "getFullClassName()"
             // DO NOT use reflection to get interfaces
             $interfaces = \Includes\Decorator\Utils\Tokenizer::getInterfaces($path);
             $class = \Includes\Decorator\Utils\Tokenizer::getFullClassName($path);
             // Do 'autoload' checking first since the class_exists tries to use autoloader
             // but fails into "cannot include file" warning when model class is not set to use (LC_Dependencies issue)
             if (\Includes\Autoloader::checkAutoload($class) && class_exists($class)) {
                 // $reflectionClass = new \ReflectionClass($class);
                 if ($class && is_subclass_of($class, '\\XLite\\Model\\AEntity')) {
                     $class = ltrim($class, '\\');
                     $len = strlen(\Includes\Utils\Database::getTablesPrefix());
                     // DO NOT remove leading backslash in interface name
                     if (in_array('\\XLite\\Base\\IDecorator', $interfaces)) {
                         $parent = \Includes\Decorator\Utils\Tokenizer::getParentClassName($path);
                         $parent = ltrim($parent, '\\');
                         $metadata = \XLite\Core\Database::getEM()->getClassMetadata($parent);
                         $table = substr($metadata->getTableName(), $len);
                         $tool = new \Doctrine\ORM\Tools\SchemaTool(\XLite\Core\Database::getEM());
                         $schema = $tool->getCreateSchemaSql(array($metadata));
                         foreach ((array) $metadata->reflFields as $field => $reflection) {
                             $pattern = '/(?:, |\\()(' . $field . ' .+)(?:, [A-Za-z]|\\) ENGINE)/USsi';
                             if ($reflection->class === $class && !empty($metadata->fieldMappings[$field]) && preg_match($pattern, $schema[0], $matches)) {
                                 $columns[$table][$field] = $matches[1];
                             }
                         }
                         foreach ($metadata->associationMappings as $mapping) {
                             if ($metadata->reflFields[$mapping['fieldName']]->class === $class) {
                                 if (isset($mapping['joinTable']) && $mapping['joinTable']) {
                                     $tables[] = substr($mapping['joinTable']['name'], $len);
                                 } elseif (isset($mapping['joinColumns']) && $mapping['joinColumns']) {
                                     foreach ($mapping['joinColumns'] as $col) {
                                         $pattern = '/(?:, |\\()(' . $col['name'] . ' .+)(?:, [A-Za-z]|\\) ENGINE)/USsi';
                                         if (preg_match($pattern, $schema[0], $matches)) {
                                             $columns[$table][$col['name']] = $matches[1];
                                         }
                                     }
                                 }
                             }
                         }
                     } elseif (\XLite\Core\Database::getRepo($class)->canDisableTable()) {
                         $tables[] = substr(\XLite\Core\Database::getEM()->getClassMetadata($class)->getTableName(), $len);
                         $metadata = \XLite\Core\Database::getEM()->getClassMetadata($class);
                         foreach ($metadata->associationMappings as $mapping) {
                             if (isset($mapping['joinTable']) && $mapping['joinTable']) {
                                 $tables[] = substr($mapping['joinTable']['name'], $len);
                             }
                         }
                     }
                 }
             }
         }
     }
     return array('tables' => $tables, 'columns' => $columns);
 }
Exemple #8
0
 /**
  * Get DB schema
  *
  * @param string $mode Schema generation mode OPTIONAL
  *
  * @return array(string)
  */
 public function getDBSchema($mode = self::SCHEMA_CREATE)
 {
     $tool = new \Doctrine\ORM\Tools\SchemaTool(static::$em);
     $schemas = array();
     $rawSchemas = null;
     $postprocessMethod = null;
     if (static::SCHEMA_CREATE == $mode) {
         $rawSchemas = $tool->getCreateSchemaSql($this->getAllMetadata());
         $postprocessMethod = 'postprocessCreateSchema';
     } elseif (static::SCHEMA_UPDATE == $mode) {
         $rawSchemas = $tool->getUpdateSchemaSql($this->getAllMetadata());
         $postprocessMethod = 'postprocessUpdateSchema';
     } elseif (static::SCHEMA_DELETE == $mode) {
         $rawSchemas = $tool->getDropSchemaSql($this->getAllMetadata());
         $postprocessMethod = 'postprocessDropSchema';
     }
     if ($rawSchemas) {
         foreach ($rawSchemas as $schema) {
             // $postprocessMethod detected by $mode
             $schema = $this->{$postprocessMethod}($schema);
             if (is_array($schema)) {
                 $schemas = array_merge($schemas, $schema);
             } elseif (isset($schema) && $schema) {
                 $schemas[] = $schema;
             }
         }
         foreach (static::$em->getMetadataFactory()->getAllMetadata() as $cmd) {
             if (!$cmd->isMappedSuperclass) {
                 $schemas = static::getRepo($cmd->name)->processSchema($schemas, $mode);
             }
         }
         $schemas = array_map('trim', $schemas);
         $schemas = preg_grep('/^.+$/Ss', $schemas);
     }
     return $schemas;
 }
Exemple #9
0
 /**
  * Returns schema from doctrine
  *
  * @param string $mode Schema generation mode OPTIONAL
  *
  * @return array
  */
 protected function getDBSchemaFromDoctrine($mode = self::SCHEMA_CREATE)
 {
     $tool = new \Doctrine\ORM\Tools\SchemaTool(static::$em);
     switch ($mode) {
         case static::SCHEMA_CREATE:
             $result = $tool->getCreateSchemaSql($this->getAllMetadata());
             break;
         case static::SCHEMA_UPDATE:
             $result = $tool->getUpdateSchemaSql($this->getAllMetadata());
             break;
         case static::SCHEMA_DELETE:
             $result = $tool->getDropSchemaSql($this->getAllMetadata());
             break;
         default:
             $result = array();
     }
     return array_unique($result);
 }