Example #1
0
 /**
  * 通过表的实体类,获取表的概要描述,包括:表名、主键、自增字段、字段、默认值
  * 应该根据不同的数据库类型创建对应的TableSchema类:$dbType = $this->getDriver(false)->getDbType();
  * 这里只用到MySQL数据库,暂时不做多数据库类型
  * @param string $tableName
  * @return \tfc\db\TableSchema
  */
 public function getTableSchema($tableName)
 {
     $className = 'tfc\\db\\TableSchema::' . strtolower($tableName);
     if (Singleton::has($className)) {
         return Singleton::get($className);
     }
     $ref = $this->getRefClass($tableName);
     $attributes = $ref->getDefaultProperties();
     $tableSchema = new TableSchema();
     $tableSchema->name = $ref->hasConstant('TABLE_NAME') ? $ref->getConstant('TABLE_NAME') : $ref->getShortName();
     $tableSchema->autoIncrement = $ref->hasConstant('AUTO_INCREMENT') ? $ref->getConstant('AUTO_INCREMENT') : null;
     if (isset($attributes['primaryKey'])) {
         $tableSchema->primaryKey = $attributes['primaryKey'];
         unset($attributes['primaryKey']);
     }
     $tableSchema->columnNames = array_keys($attributes);
     if ($tableSchema->primaryKey === null) {
         $tableSchema->primaryKey = $tableSchema->columnNames[0];
     }
     foreach ($attributes as $key => $value) {
         if ($value === null) {
             unset($attributes[$key]);
         }
     }
     $tableSchema->attributeDefaults = $attributes;
     Singleton::set($className, $tableSchema);
     return $tableSchema;
 }