Esempio n. 1
0
 /**
  * @return Schema
  */
 public function init()
 {
     $this->getDb()->query('USE ' . $this->getName());
     $sql = 'show tables';
     $tableNames = array();
     $_tableNames = $this->_db->fetchAll($sql);
     uasort($_tableNames, function ($a, $b) {
         $v = reset($a);
         $vv = reset($b);
         if (substr($v, -5) == '_link' && substr($vv, -5) == '_link') {
             if (substr_count($v, '_') == substr_count($vv, '_')) {
                 return strcmp($v, $vv);
             } else {
                 return substr_count($v, '_') > substr_count($vv, '_');
             }
         } elseif (substr($v, -5) == '_link') {
             return true;
         } else {
             if (substr_count($v, '_') == substr_count($vv, '_')) {
                 return strcmp($v, $vv);
             } else {
                 return substr_count($v, '_') > substr_count($vv, '_');
             }
         }
     });
     foreach ($_tableNames as $name) {
         $tableNames[] = reset($name);
     }
     if (!empty($tableNames)) {
         foreach ($tableNames as $tableName) {
             if ($tableName[0] == '_') {
                 continue;
             }
             $table = new Table($tableName, $this);
             $this->addTable($table->init());
         }
     }
     $this->initIndex();
     /** @var Schema|Table[] $this */
     foreach ($this as $table) {
         /** @var Table|Column[] $table */
         foreach ($table as $column) {
             $column->init();
         }
     }
     $this->initTableLinks();
     return $this;
 }