Пример #1
0
 /**
  * List database tables
  *
  * @param  bool $all
  * @return void
  */
 protected function listTables($all = false)
 {
     $config = Tools::getConfig();
     $connection = Tools::getConnection();
     if ($all) {
         $tables = array('all' => 'All');
     } else {
         $tables = array();
     }
     $dbTables = $connection->listTables();
     foreach ($dbTables as $dbTable) {
         $tables[$dbTable] = $dbTable;
     }
     $this->view->tables = $tables;
     if ($config->database->adapter != 'Sqlite') {
         $this->view->databaseName = $config->database->dbname;
     } else {
         $this->view->databaseName = null;
     }
 }
Пример #2
0
 /**
  * Build Models
  * @throws \Exception
  */
 public function build()
 {
     if (isset($this->_options['defineRelations'])) {
         $defineRelations = $this->_options['defineRelations'];
     } else {
         $defineRelations = false;
     }
     if (isset($this->_options['foreignKeys'])) {
         $defineForeignKeys = $this->_options['foreignKeys'];
     } else {
         $defineForeignKeys = false;
     }
     if (isset($this->_options['genSettersGetters'])) {
         $genSettersGetters = $this->_options['genSettersGetters'];
     } else {
         $genSettersGetters = false;
     }
     if (isset(Tools::getConfig()->database)) {
         $dbConfig = Tools::getConfig()->database;
     } elseif (Tools::getConfig()->db) {
         $dbConfig = Tools::getConfig()->db;
     }
     if (isset($dbConfig->adapter)) {
         $adapter = $dbConfig->adapter;
         $this->isSupportedAdapter($adapter);
     } else {
         $adapter = 'Mysql';
     }
     if (is_object($dbConfig)) {
         $configArray = $dbConfig->toArray();
     } else {
         $configArray = $dbConfig;
     }
     $adapterName = 'Phalcon\\Db\\Adapter\\Pdo\\' . $adapter;
     unset($configArray['adapter']);
     /**
      * @var $db \Phalcon\Db\Adapter\Pdo
      */
     $db = new $adapterName($configArray);
     if (isset($this->_options['schema'])) {
         $schema = $this->_options['schema'];
     } elseif ($adapter == 'Postgresql') {
         $schema = 'public';
     } else {
         $schema = isset($dbConfig->schema) ? $dbConfig->schema : $dbConfig->dbname;
     }
     $hasMany = array();
     $belongsTo = array();
     $foreignKeys = array();
     if ($defineRelations || $defineForeignKeys) {
         foreach ($db->listTables($schema) as $name) {
             if ($defineRelations) {
                 if (!isset($hasMany[$name])) {
                     $hasMany[$name] = array();
                 }
                 if (!isset($belongsTo[$name])) {
                     $belongsTo[$name] = array();
                 }
             }
             if ($defineForeignKeys) {
                 $foreignKeys[$name] = array();
             }
             $camelCaseName = Text::camelize($name);
             $refSchema = $adapter != 'Postgresql' ? $schema : $dbConfig->dbname;
             foreach ($db->describeReferences($name, $schema) as $reference) {
                 $columns = $reference->getColumns();
                 $referencedColumns = $reference->getReferencedColumns();
                 $referencedModel = Text::camelize($reference->getReferencedTable());
                 if ($defineRelations) {
                     if ($reference->getReferencedSchema() == $refSchema) {
                         if (count($columns) == 1) {
                             $belongsTo[$name][] = array('referencedModel' => $referencedModel, 'fields' => $columns[0], 'relationFields' => $referencedColumns[0], 'options' => $defineForeignKeys ? array('foreignKey' => true) : NULL);
                             $hasMany[$reference->getReferencedTable()][] = array('camelizedName' => $camelCaseName, 'fields' => $referencedColumns[0], 'relationFields' => $columns[0]);
                         }
                     }
                 }
             }
         }
     } else {
         foreach ($db->listTables($schema) as $name) {
             if ($defineRelations) {
                 $hasMany[$name] = array();
                 $belongsTo[$name] = array();
                 $foreignKeys[$name] = array();
             }
         }
     }
     foreach ($db->listTables($schema) as $name) {
         $className = Text::camelize($name);
         if (!file_exists($this->_options['directory'] . DIRECTORY_SEPARATOR . $className . '.php') || $this->_options['force']) {
             if (isset($hasMany[$name])) {
                 $hasManyModel = $hasMany[$name];
             } else {
                 $hasManyModel = array();
             }
             if (isset($belongsTo[$name])) {
                 $belongsToModel = $belongsTo[$name];
             } else {
                 $belongsToModel = array();
             }
             if (isset($foreignKeys[$name])) {
                 $foreignKeysModel = $foreignKeys[$name];
             } else {
                 $foreignKeysModel = array();
             }
             $modelBuilder = new Model(array('module' => $this->_options['module'], 'name' => $className, 'tableName' => $name, 'schema' => $schema, 'baseClass' => $this->_options['baseClass'], 'namespace' => $this->_options['namespace'], 'force' => $this->_options['force'], 'hasMany' => $hasManyModel, 'belongsTo' => $belongsToModel, 'foreignKeys' => $foreignKeysModel, 'genSettersGetters' => $genSettersGetters, 'directory' => $this->_options['directory']));
             $modelBuilder->build();
         } else {
             $this->exist[] = $className;
         }
     }
 }
 /**
  * Run Migrations
  */
 public function runAction()
 {
     if ($this->request->isPost()) {
         $version = '';
         $exportData = '';
         $force = $this->request->getPost('force', 'int');
         try {
             $migrationsDir = $this->_getMigrationsDir();
             Migrations::run(array('config' => Tools::getConfig(), 'directory' => null, 'tableName' => 'all', 'migrationsDir' => $migrationsDir, 'force' => $force));
             $this->flash->success("The migration was executed successfully");
         } catch (\Exception $e) {
             $this->flash->error($e->getMessage());
         }
     }
     $this->_prepareVersions();
 }