<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ## 創建core/database/model/Model.php ``` <?php namespace core\database\model; class Model { // 綁定的數據庫連接 protected $connection; protected $table; // 表 protected $paimaryKey; // 主鍵 protected $timestamps = true; // 是否自動維護時間字段 /* 為什么要分開兩個屬性? $orginal 原的數據 $attriubte 原數據的復制版 用戶只能修改這個 ! 然后跟$original相比較 得出用戶修改的數據字段 */ protected $original; protected $attribute; public function __construct() { // 給當前模型綁定一個數據庫連接 $this->connection = \App::getContainer()->get('db')->connection( $this->connection ); } // 獲取表名稱 沒有表名稱 就返回 模型(小寫)+s public function getTable() { if( $this->table) return $this->table; $class_name = get_class($this); $class_arr = explode('\\',$class_name); $table = lcfirst(end( $class_arr )); return $table .'s'; } public function setOriginalValue($key, $val) { if(! $this->original) $this->original = new \stdClass(); $this->original->$key = $val; } public function setAttribute($key, $val) { if(! $this->attribute) $this->attribute = new \stdClass(); $this->attribute->$key = $val; } // 見最上面的說明 public function __set($key, $value) { $this->setAttribute($key, $value); } // 屬性 同步 original public function syncOriginal() { $this->attribute = $this->original; } /** * 返回用戶改過的數據 * @return array * @example ['id' => 3,'user_id' => '3'] */ public function diff() { $diff = []; if( $this->attribute == $this->original) // 沒改變 return $diff; foreach ($this->original as $origin_key => $origin_val) if( $this->attribute->$origin_key != $origin_val) // 改變了 $diff[$origin_key] = $this->attribute->$origin_key; return $diff; } public function __get($name) { return $this->attribute->$name; } // 托管到 __call //因此: User::where() 與 (new User)->where() 是一樣的 public static function __callStatic($method, $args) { return (new static())->$method(...$args); } public function __call($method, $args) { return (new Builder( $this->connection->newBuilder() )) ->setModel($this) ->$method(...$args); } } ```` ## 創建core/database/model/Builder.php ``` <?php namespace core\database\model; class Builder { protected $query; protected $model; public function __construct($query) { $this->query = $query; } public function setModel(Model $model) { $this->model = $model; return $this; } public function __call($method, $args) { $this->query->$method(...$args); return $this; } public function get($columns = ['*']) { if(! is_array($columns)) $columns = func_get_args(); $this->query->columns = $columns; $this->query->table( $this->model->getTable()); $sql = $this->query->toSql(); return $this->bindModel( $this->query->runSql($sql) ); } // 數據映射模式 把數據映射到模型 //模型的本質: 每條數據都是一個模型(對象) protected function bindModel($datas) { if(! is_array($datas)) $datas[] = $datas; $models = []; foreach ($datas as $data){ // 多少條數據就多少個模型 $model = clone $this->model; // 原型模式 foreach ($data as $key => $val) $model->setOriginalValue($key, $val); $model->syncOriginal(); // 把attriubtes = orginal $models[] = $model; } return $models; } } ``` ## 編輯app/User.php 繼承模型基礎類 ![](https://img.kancloud.cn/a4/69/a469507e60982a6a0754d7e25682e78a_1311x630.png) ## 運行 由于每條數據都是一個類,所以可以調用方法。 ![](https://img.kancloud.cn/8c/ed/8cedf0e02c378211d95918bf102adc13_553x156.png) ![](https://img.kancloud.cn/29/b3/29b33c1d7d2d2eec98d116a89372dd80_595x268.png)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看