<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、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                | 版本 | 功能調整 | | --- | --- | | 5.0.5 | 原生查詢不支持返回數據集對象 | 默認的結果類型是數組 ``` 'resultset_type' => 'array', ``` ### **獲取單條數據** Db類find返回數組 ``` $users = \think\Db::name('user')->find(1); ``` ![](https://img.kancloud.cn/ea/0e/ea0ec86f16ea886835f6e9fda62ef56d_169x93.png) 返回Model模型對象(這里是User模型對象) 模型對象數據庫查出的值在data屬性里, 通過__get動態獲取字段的值 ``` $users1 = model('user')->find(1); $users2 = model('user')::find(1); $users3 = model('user')::get(1); echo $users1->id; echo $users2->id; echo $users3->id; ``` ![](https://img.kancloud.cn/70/0d/700d8ae3853a64454e48d91c80b84de4_255x114.png) 模型對象可以直接使用toArray()轉成數組 ``` $user1=$users1->toArray() ``` ![](https://img.kancloud.cn/1a/8f/1a8fee428b39306833b0e6fffa943cde_192x87.png) ### **獲取多條數據** select和All默認返回模型組成的數組 ``` $users1 = \think\Db::name('user')->select(); $users2 = model('user')::all(); $users3 = \app\index\model\User::all(); ``` ![](https://img.kancloud.cn/8a/fc/8afc302fb3391c286bf5e5707592be20_284x312.png) 可知select和All在` 'resultset_type' => 'array',`的情況下是無法直接使用toArray的 我們可以將User模型遍歷出來在進行toArray()進行轉化,第二種辦法就是直接將數據轉為數據集類在進行toArray() ``` collection($users3)->toArray() ``` ![](https://img.kancloud.cn/1b/e8/1be81f534a5bcc28dfc77fb398779ab1_140x98.png) ### **讓select和All默認返回數據集類** 數據庫的查詢結果也就是數據集,database.php默認的配置下,數據集的類型是一個二維數組,我們可以配置成數據集類,就可以支持對數據集更多的對象化操作,需要使用數據集類功能,可以有以下幾種方法 **1、database.php配置數據庫的`resultset_type`參數如下:** ~~~ return [ // 數據集返回類型 注意這里的值是全小寫的collection,或者`'\think\Collection'` 'resultset_type' => 'collection', ]; ~~~ **2、或者在模型中定義resultSetType 屬性** ~~~ protected $resultSetType = 'collection'; ~~~ **3、V5.1.23+ 版本開始,你可以在查詢的時候指定是否需要返回數據集(無需配置 resultset\_type 參數)** ~~~ // 獲取數據集 $users = Db::name('user')->fetchCollection()->select(); ~~~ 配置完成后返回的數據集對象是`think\Collection`, ``` $users1 = \think\Db::name('user')->select(); $users2 = model('user')::all(); $users3 = \app\index\model\User::all(); ``` ![](https://img.kancloud.cn/7e/38/7e38fd900adb7a71611dd904088ab011_195x190.png) `think\Collection`提供了和數組無差別用法,并且另外封裝了一些額外的方法。、 可以直接使用數組的方式操作數據集對象,例如: ~~~ // 獲取數據集 $users = \think\Db::name('user')->select(); // 直接操作第一個元素結果直接為數組了哦 $item = $users[0]; dump($item); // 獲取數據集記錄數 $count = count($users);//3 // 遍歷數據集 foreach($users as $user){ //遍歷后的$user也是數組了 echo $user['username'].'<--->'; echo $user['id'].'@<br>'; } //數據集直接轉為數組 $users_arr=$users->toArray(); dump($users_arr); ~~~ ![](https://img.kancloud.cn/94/dc/94dc3c2c93d58ccd1d9b22e78396afa9_211x280.png) 需要注意的是,如果要判斷數據集是否為空,不能直接使用`empty`判斷,而必須使用數據集對象的`isEmpty`方法判斷,例如: ~~~ $users = \think\Db::name('user')->select(); if($users->isEmpty()){ echo '數據集為空'; } ~~~ ## **總結:** >[info]find() 查詢結果必為 一維數組 value/column 查詢結果必為 int/string/一維數組 select與All 當配置resultset\_type => collection 時, 查詢結果為 collection數據集 select 當配置resultset\_type => array時,如果用Db::類查詢為二維數組,但如果用Model類查詢為Model對象(使用不當會報錯,因為它是個對象)可用collection($Model);轉為數據集 結論: Query查詢單條數據返回數組,Model查詢單條數據返回的模型類可以直接使用toArray()轉數組 Query和Model查詢多條數據時 ## **數據集方法操作** **Model:** ``` // 獲取數據集 $users = model('user')->select(); if ($users instanceof \think\Collection && !$users->isEmpty()) { //獲取全部的數據 dump($users->all()); dump($users->toArray()); } ``` ![](https://img.kancloud.cn/aa/19/aa19c34ed659a1ee0ece0d435adc904c_172x205.png) 雖然返回的是Model但是還是可以向操作數組那樣操作Model里的data屬性 ``` // 獲取數據集 $users = model('user')->select(); $users = model('user')::All(); if ($users instanceof \think\Collection && !$users->isEmpty()) { //獲取全部的數據 dump($users); dump($users->all()); dump($users->column('id','username'));//column用法和array_column一致 dump($users[0]['username']); $users[0]['xixi']='haha'; dump($users->toArray()); } ``` ![](https://img.kancloud.cn/25/6c/256ce13e8ebdb1ed49e45182002ee0b0_553x476.png) ![](https://img.kancloud.cn/9c/43/9c4331b0db237218277ea6bca2d8c78d_204x83.png) **Query:** ``` // 獲取數據集 $users = \think\Db::name('user')->select(); if ($users instanceof \think\Collection && !$users->isEmpty()) { //獲取全部的數據 Query獲取多條數據時,同$users->toArray() dump($users->all()); $newUsers=$users->merge(['name'=>'dash']); dump($newUsers); $newUsers=$users->merge([['name'=>'dash','id'=>4,'pwd'=>'123','sex'=>'1']]); dump($newUsers); dump($users->all()); dump($newUsers->all()); } ``` ![](https://img.kancloud.cn/1f/7f/1f7f8e660efd08e58c064fce0ae97771_612x612.png) ``` // 獲取數據集 $users = \think\Db::name('user')->select(); if ($users instanceof \think\Collection && !$users->isEmpty()) { //獲取全部的數據 dump($users->all()); $users->pop(); dump($users->all()); } ``` ![](https://img.kancloud.cn/bf/e6/bfe6bc7d1c06d70bbd852b9bdb12beb3_199x183.png) ``` // 獲取數據集 $users = \think\Db::name('user')->select(); if ($users instanceof \think\Collection && !$users->isEmpty()) { //獲取全部的數據 dump($users->all()); $users->unshift(['name'=>'張三','id'=>4,'pwd'=>'123','sex'=>'1']); $users->unshift(['name'=>'李四','id'=>5,'pwd'=>'456','sex'=>'1'],"自定義鍵"); dump($users->all()); } ``` ![](https://img.kancloud.cn/a9/c5/a9c56fed56796619d1eecb9a47175980_247x374.png) ``` // 獲取數據集 $users = \think\Db::name('user')->select(); if ($users instanceof \think\Collection && !$users->isEmpty()) { //獲取全部的數據 dump($users->all()); dump($users->column('id'));//column用法和array_column一致 dump($users->column('id','username')); } ``` ![](https://img.kancloud.cn/4f/cf/4fcf120b932ab61b11978aca41f47f09_278x343.png) `Collection`類包含了下列主要方法: | 方法 | 描述 | | --- | --- | | isEmpty() | 是否為空 | | toArray() | 轉換為數組 | | toJson() | 轉換為json | | all() | 所有數據 | | merge($items) | 合并其它數據 | | diff($items) | 比較數組,返回差集 | | flip() | 交換數據中的鍵和值 | | intersect($items) | 比較數組,返回交集 | | keys() | 返回數據中的所有鍵名 | | pop() | 刪除數據中的最后一個元素 | | shift() | 刪除數據中的第一個元素 | | unshift($value, $key = null) | 在數據開頭插入一個元素 | | push($value, $key = null)| 在數組結尾插入一個元素 | | reduce(callable $callback, $initial = null) | 通過使用用戶自定義函數,以字符串返回數組 | | reverse() | 數據倒序重排 | | chunk($size, $preserveKeys = false) | 數據分隔為多個數據塊 | | each(callable $callback) | 給數據的每個元素執行回調 | | filter(callable $callback = null) | 用回調函數過濾數據中的元素 | | column($columnKey, $indexKey = null) | 返回數據中的指定列 | | sort(callable $callback = null) | 對數據排序 | | shuffle() | 將數據打亂 | | slice($offset, $length = null, $preserveKeys = false) | 截取數據中的一部分 |
                  <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>

                              哎呀哎呀视频在线观看