Пример #1
0
 /**
  * This function queries the database for the structure of a given class
  * and returns the structure. It is 'cached' and stored in a data structure
  * so that each model's schema is queried from the database only once per
  * active request.
  *
  * @param null $class
  *
  * @return mixed
  */
 public static function getSchema($class = null)
 {
     $modelName = static::getModelName($class);
     $tableName = static::getTableName($class);
     return static::$_schema->get($modelName, function ($modelName, $tableName) {
         $result = $modelName::queryBuilder()->describe($tableName)->executeAndFetchAll();
         $schema = array();
         foreach ($result as $info) {
             switch (static::$_db->getType()) {
                 case 'mysql':
                     $schema[$info->Field] = array('type' => $info->Type, 'null' => $info->Null, 'key' => $info->Key, 'default' => $info->Default);
                     break;
                 case 'sqlite3':
                     $schema[$info->name] = array('type' => $info->type, 'null' => $info->notnull ? false : true, 'key' => $info->pk ? true : false, 'default' => $info->dflt_value);
                     break;
             }
         }
         return $schema;
     }, array($modelName, $tableName));
 }