Exemple #1
0
 protected function onConstruct($argv = null)
 {
     $this->src = App::getApp()->src();
     $this->adapter = Db::getAdapter($this->source);
     $this->className = str_replace('/', '\\', $this->className);
     list($this->namespace, $this->className) = parse_class($this->className);
     if (empty($this->tableName)) {
         $this->tableName = strtolower($this->className);
     }
     $this->tableName = Db::mkTableName($this->source, $this->className, $this->tableName);
 }
Exemple #2
0
 public static function initModel()
 {
     if (!static::$_init) {
         $isInit = true;
         $class = static::class;
         // todo: 这里要增加从缓存中读取
         // 初始化表名
         $table = Db::mkTableName(static::$dbSource, $class, static::$tableName, static::$tablePrefix);
         static::$tableName =& $table;
         $filter = static::getFilter();
         $groupColumns = static::$groupColumns;
         $pk = null;
         $pkAutoInc = false;
         $dbColumns = static::dbColumns();
         $columns = static::$columns;
         // =============================================================== //
         // user : [a, b] + db : [b, c]
         // [a(user), b(user), c(db)], b(db)
         // when
         // b(db) => [ a => 1, b => 2 ]
         // b(user) => [ a => 2, c => 3 ]
         // b(user) + b(db) => [ a => 2, c => 3, b => 2 ] (drop a => 1)
         // =============================================================== //
         // 数据库字段(dbColumns),全部由脚本读取数据库生成,他不包含任何用户手动定义的内容。
         // 但用户定义的字段(static $columns),则由用户手工输入,包含了编辑类型、特定的属性声明,默认值等。
         // 所以我们以用户定义的字段作为默认基础
         // =============================================================== //
         foreach ($columns + $dbColumns as $field => $column) {
             if (is_string($column)) {
                 $column = ['label' => $column];
             } elseif (!is_array($column)) {
                 $column = [];
             }
             if (isset($columns[$field]) && isset($dbColumns[$field])) {
                 $column += $dbColumns[$field];
             }
             if (!empty($column['pk']) && empty($pk)) {
                 $pk = $field;
                 $pkAutoInc = $column['autoInc'] ?? false;
             }
             $columns[$field] = $filter->initColumn($field, $column, $groupColumns, false);
         }
         // 全部字段过滤完以后,才来检查主键的有效性
         if (empty($pk)) {
             $pk = trim(static::$pk);
             $pkAutoInc = static::$pkAutoInc;
             if (!isset($columns[$pk])) {
                 $columns[$pk] = [];
             }
         }
         $columns[$pk] = $filter->initColumn($pk, $columns[$pk], $groupColumns, true, $pkAutoInc);
         if (static::isEnableCache()) {
             $fields = array_keys($dbColumns);
             sort($fields, SORT_ASC | SORT_STRING);
             $groupColumns['columnsHash'] = hash('crc32b', implode('|', $fields));
         }
         static::$pk =& $pk;
         static::$pkAutoInc =& $pkAutoInc;
         static::$columns =& $columns;
         static::$groupColumns =& $groupColumns;
         static::$_init =& $isInit;
         static::onInitModel();
     }
 }