# ThinkPHP6.0 數據庫鏈式操作
數據庫提供的鏈式操作方法,可以有效的提高數據存取的代碼清晰度和開發效率,并且支持所有的CURD操作。
* * *
## ThinkPHP6 數據庫鏈式操作
* 數據庫提供的鏈式操作方法,可以有效的提高數據存取的代碼清晰度和開發效率,并且支持所有的`CURD`操作
* 帶\*標識的表示支持多次調用
連貫操作 作用 支持的參數類型where*用于AND查詢字符串、數組和對象table 用于定義要操作的數據表名稱字符串和數組name 用于定義要操作的數據表名稱字符串field*用于定義要查詢的字段(支持字段排除)字符串和數組order*用于對結果排序字符串和數組limit 用于限制查詢結果數量字符串和數字page 用于查詢分頁(內部會轉換成limit)字符串和數字
## 一、表達式查詢
* 表達式是SQL語句的條件
* 表達式不分大小寫
* 表達式寫在where里
表達式含義查詢方法=等于<>不等于>大于>=大于等于<小于<=小于等于[NOT] LIKE模糊查詢whereLike/whereNotLike[NOT] BETWEEN(不在)區間查詢whereBetween/whereNotBetween[NOT] IN(不在)IN 查詢 whereIn/whereNotIn[NOT] NULL查詢字段是否(不)是NULLwhereNull/whereNotNull
`where`查詢
* where方法在鏈式操作方法里面是最常用的方法,可以完成包括普通查詢、表達式查詢、快捷查詢、區間查詢、組合查詢在內的條件查詢操作
> \# 等于(=)
>
> $select = Db::table('shop\_goods')->where('id','=','1')->select();
>
> print\_r($select->toArray());
>
>
>
> \# 不等于(<>)
>
> $select = Db::table('shop\_goods')->where('id','select();
>
> print\_r($select->toArray());
>
>
>
> \# 大于(>)
>
> $select = Db::table('shop\_goods')->where('id','>','3')->select();
>
> print\_r($select->toArray());
>
>
>
> \# 大于等于(>=)
>
> $select = Db::table('shop\_goods')->where('id','>=','4')->select();
>
> print\_r($select->toArray());
>
>
>
> \# 小于(<)
>
> $select = Db::table('shop\_goods')->where('id','select();
>
> print\_r($select->toArray());
>
>
>
> \# 小于等于(<=)
>
> $select = Db::table('shop\_goods')->where('id','select();
>
> print\_r($select->toArray());
>
>
>
> \# 多where
>
> $select = Db::table('shop\_goods')
>
> ? ? ? ? ? ? ->where('id','>','3')
>
> ? ? ? ? ? ? ->where('id','<','8')
>
> ? ? ? ? ? ? ->select();
>
> print\_r($select->toArray());
>
>
>
> \# LIKE
>
> $select = Db::table('shop\_goods')->where('title','like','%連衣裙%')->select();
>
> print\_r($select->toArray());
>
>
>
> #? NOT LIKE
>
> $select = Db::table('shop\_goods')->where('title','not like','%連衣裙%')->select();
>
> print\_r($select->toArray());
>
>
>
> \# BETWEEN
>
> $select = Db::table('shop\_goods')->where('id','between','6,10')->select();
>
> print\_r($select->toArray());
>
>
>
> #? NOT BETWEEN
>
> $select = Db::table('shop\_goods')->where('id','not between',\[6,10\])->select();
>
> print\_r($select->toArray());
>
>
>
> \# IN
>
> $select = Db::table('shop\_goods')->where('id','in','4,7,10')->select();
>
> print\_r($select->toArray());
>
>
>
> #? NOT IN
>
> $select = Db::table('shop\_goods')->where('id','not in',\[4,7,10\])->select();
>
> print\_r($select->toArray());
## 二、數據表
1、table 和 name
> \# 必須完整數據庫名
>
> $select = Db::table('shop\_goods')->where('id','10')->select();
>
> print\_r($select->toArray());
>
> \# 數據庫未設置前綴
>
> $select = Db::name('shop\_goods')->where('id','11')->select();
>
> print\_r($select->toArray());
>
> \# 數據庫設置前綴,無前綴訪問
>
> $select = Db::name('list')->where('id','12')->select();
>
> print\_r($select->toArray());
2、數據庫前綴
數據庫配置`database.php`
> return \[
>
> ? ? 'connections'? ? ?=> \[
>
> ? ? ? ? 'mysql' => \[
>
> ? ? ? ? ? ? // 數據庫表前綴
>
> ? ? ? ? ? ? 'prefix'? => Env::get('database.prefix', 'shop\_'),
>
> ? ? ? ? \]
>
> ? ? \]
>
> \];
## 三、返回值
1、`field?`
* field 方法主要作用是標識要返回或者操作的字段,可以用于查詢和寫入操作
* 所有的查詢方法都可以使用field方法
> \# 字符串
>
> $select = Db::table('shop\_goods')
>
> ? ? ? ? ? ? ->field('title,price,discount as d')
>
> ? ? ? ? ? ? ->where('status',1)
>
> ? ? ? ? ? ? ->select();
>
> print\_r($select->toArray());
>
>
>
> \# 數組
>
> $select = Db::table('shop\_goods')
>
> ? ? ? ? ? ? ->field(\[
>
> ? ? ? ? ? ? ? ? 'title',
>
> ? ? ? ? ? ? ? ? 'price',
>
> ? ? ? ? ? ? ? ? 'discount'=>'d'
>
> ? ? ? ? ? ? \])
>
> ? ? ? ? ? ? ->where('status',1)
>
> ? ? ? ? ? ? ->select();
>
> print\_r($select->toArray());
>
>
>
> \# 添加,只能添加這幾個字段
>
> \# 多field
>
> $data = \[
>
> ? ? 'title' => '新商品',
>
> ? ? 'price' => 50,
>
> ? ? 'discount' => 8,
>
> ? ? 'add\_time' => 1576080000
>
> \];
>
> $insert = Db::table('shop\_goods')
>
> ? ? ? ? ? ? ->field('title')
>
> ? ? ? ? ? ? ->field('price')
>
> ? ? ? ? ? ? ->field('discount')
>
> ? ? ? ? ? ? ->field('add\_time')
>
> ? ? ? ? ? ? ->insert($data);
>
> print\_r($insert);
>
>
>
> \# 查詢全部字段,速度較快
>
> $select = Db::table('shop\_goods')
>
> ? ? ? ? ? ? ->field(true)
>
> ? ? ? ? ? ? // ->field('\*')
>
> ? ? ? ? ? ? ->where('status',1)
>
> ? ? ? ? ? ? ->select();
>
> print\_r($select->toArray());
2、`withoutField`
* withoutField 方法作用 排除數據表中的字段
~~~
Db::table('shop_goods')->withoutField('id')->select();
~~~
3、`fieldRaw`
* fieldRaw 方法直接使用mysql函數
~~~
Db::table('shop_goods')->fieldRaw('id,sum(price)')->select();
~~~
## 四、排序
1、`order`方法用于對操作的結果排序或者優先級限制
* 默認正序
* asc 正序
* desc 倒序
> $select = Db::table('shop\_goods')
>
> ? ? ? ? ? ? ->field('title,price,id')
>
> ? ? ? ? ? ? ->where('status',1)
>
> ? ? ? ? ? ? ->order('price','DESC')
>
> ? ? ? ? ? ? ->order('id','DESC')
>
> ? ? ? ? ? ? ->select();
>
> print\_r($select->toArray());
2、`orderRaw`方法中使用mysql函數
> $select = Db::table('shop\_goods')
>
> ? ? ? ? ? ? ->field('title,price,id')
>
> ? ? ? ? ? ? ->where('status',1)
>
> ? ? ? ? ? ? ->orderRaw("field(title,'price','discount','stock')")
>
> ? ? ? ? ? ? ->select();
>
> print\_r($select->toArray());
## 五、分頁
* `limit`方法主要用于指定查詢和操作的數量
> $select = Db::table('shop\_goods')
>
> ? ? ? ? ? ? ->field('title,price,id')
>
> ? ? ? ? ? ? ->where('status',1)
>
> ? ? ? ? ? ? ->order('price','DESC')
>
> ? ? ? ? ? ? ->limit(3)
>
> ? ? ? ? ? ? ->select();
>
> print\_r($select->toArray());
>
>
>
> $select = Db::table('shop\_goods')
>
> ? ? ? ? ? ? ->field('title,price,id')
>
> ? ? ? ? ? ? ->where('status',1)
>
> ? ? ? ? ? ? ->order('price','DESC')
>
> ? ? ? ? ? ? ->limit(0,5)
>
> ? ? ? ? ? ? ->select();
>
> print\_r($select->toArray());
* `page`方法主要用于分頁查詢
> $select = Db::table('shop\_goods')
>
> ? ? ? ? ? ? ->field('title,price,id')
>
> ? ? ? ? ? ? ->where('status',1)
>
> ? ? ? ? ? ? ->order('price','DESC')
>
> ? ? ? ? ? ? ->page(1,5)
>
> ? ? ? ? ? ? ->select();
>
> print\_r($select->toArray());
## 六、聚合查詢
* 聚合方法如果沒有數據,默認都是0,聚合查詢都可以配合其它查詢條件
方法功能count 統計數量,參數是要統計的字段名(可選)max 獲取最大值,參數是要統計的字段名(必須)min 獲取最小值,參數是要統計的字段名(必須)avg 獲取平均值,參數是要統計的字段名(必須)sum獲取總數,參數是要統計的字段名(必須)
> // 統計數量,參數是要統計的字段名(可選)
>
> $select = Db::table('shop\_goods')->count();
>
> print\_r($select);
>
>
>
> // 獲取最大值,參數是要統計的字段名(必須)
>
> $select = Db::table('shop\_goods')->max('id');
>
> print\_r($select);
>
>
>
> // 獲取最小值,參數是要統計的字段名(必須)
>
> $select = Db::table('shop\_goods')->min('id');
>
> print\_r($select);
>
>
>
> // 獲取平均值,參數是要統計的字段名(必須)
>
> $select = Db::table('shop\_goods')->avg('id');
>
> print\_r($select);
>
>
>
> // 獲取總數,參數是要統計的字段名(必須)
>
> $select = Db::table('shop\_goods')->sum('id');
>
> print\_r($select);
## 七、搜索、排序示例
controller代碼
> public function index(){
>
> ? ? $title = '商城';
>
> ? ? $login = '歐陽克';
>
> ? ? # 左側菜單
>
> ? ? $menu = Db::table('shop\_menu')->where('fid',0)->select();
>
> ? ? $left = $menu->toArray();
>
> ? ? foreach($left as &$left\_v){
>
> ? ? ? ? $left\_v\['lists'\] = Db::table('shop\_menu')->where('fid',$left\_v\['id'\])->select();
>
> ? ? }
>
> ? ? # 右側列表
>
> ? ? $param = Request::param();
>
> ? ? if(isset($param\['status'\]) && $param\['status'\] == 1){
>
> ? ? ? ? $where\['status'\] = 1;
>
> ? ? }else if(isset($param\['status'\]) && $param\['status'\] == 2){
>
> ? ? ? ? $where\['status'\] = 2;
>
> ? ? }else{
>
> ? ? ? ? $where = true;
>
> ? ? }
>
> ? ? $list = Db::table('shop\_goods')
>
> ? ? ? ? ? ? ? ? ->where($where)
>
> ? ? ? ? ? ? ? ? ->order('add\_time DESC')
>
> ? ? ? ? ? ? ? ? ->order('id DESC')
>
> ? ? ? ? ? ? ? ? ->select();
>
> ? ? $right = $list->toArray();
>
> ? ? foreach($right as &$right\_v){
>
> ? ? ? ? $right\_v\['cat'\] = Db::table('shop\_cat')->where('id',$right\_v\['cat'\])->value('name');
>
> ? ? }
>
> ? ? View::assign(\[
>
> ? ? ? ? 'title'? => $title,
>
> ? ? ? ? 'login' => $login,
>
> ? ? ? ? 'left' => $left,
>
> ? ? ? ? 'right' => $right,
>
> ? ? ? ? 'status' => isset($param\['status'\]) ? $param\['status'\] : null
>
> ? ? \]);
>
> ? ? return View::fetch();
>
> }
view代碼
>
>
> ? ?
>
> ? ? ? ?
>
> ? ? ? ? ? ?
>
> ? ? ? ? ? ? ? ? 全部
>
> ? ? ? ? ? ? ? ? 開啟
>
> ? ? ? ? ? ? ? ? 關閉
>
> ? ? ? ? ? ?
>
> ? ? ? ?
>
> ? ? ? ? 搜索
>
> ? ?
>
>
## 八、分頁示例
controller代碼
> public function index(){
>
> ? ? $title = '商城';
>
> ? ? $login = '歐陽克';
>
> ? ? # 左側菜單
>
> ? ? $menu = Db::table('shop\_menu')->where('fid',0)->select();
>
> ? ? $left = $menu->toArray();
>
> ? ? foreach($left as &$left\_v){
>
> ? ? ? ? $left\_v\['lists'\] = Db::table('shop\_menu')->where('fid',$left\_v\['id'\])->select();
>
> ? ? }
>
> ? ? # 右側列表
>
> ? ? $param = Request::param();
>
> ? ? if(isset($param\['status'\]) && $param\['status'\] == 1){
>
> ? ? ? ? $where\['status'\] = 1;
>
> ? ? }else if(isset($param\['status'\]) && $param\['status'\] == 2){
>
> ? ? ? ? $where\['status'\] = 2;
>
> ? ? }else{
>
> ? ? ? ? $where = true;
>
> ? ? }
>
> ? ? $p = isset($param\['p'\]) ? $param\['p'\] : 1;
>
> ? ? // 統計總數
>
> ? ? $count = Db::table('shop\_goods')->where($where)->count();
>
> ? ? $list = Db::table('shop\_goods')
>
> ? ? ? ? ? ? ? ? ->where($where)
>
> ? ? ? ? ? ? ? ? ->order('add\_time DESC')
>
> ? ? ? ? ? ? ? ? ->order('id DESC')
>
> ? ? ? ? ? ? ? ? ->page($p,10)
>
> ? ? ? ? ? ? ? ? ->select();
>
> ? ? $right = $list->toArray();
>
> ? ? foreach($right as &$right\_v){
>
> ? ? ? ? $right\_v\['cat'\] = Db::table('shop\_cat')->where('id',$right\_v\['cat'\])->value('name');
>
> ? ? }
>
> ? ? View::assign(\[
>
> ? ? ? ? 'title'? => $title,
>
> ? ? ? ? 'login' => $login,
>
> ? ? ? ? 'left' => $left,
>
> ? ? ? ? 'right' => $right,
>
> ? ? ? ? 'count' => ceil($count/10),
>
> ? ? ? ? 'p' => $p,
>
> ? ? ? ? 'status' => isset($param\['status'\]) ? $param\['status'\] : 0
>
> ? ? \]);
>
> ? ? return View::fetch();
>
> }
view代碼
>
>
> ? ? 上一頁
>
> ? ? {for start="0" end="$count"}
>
> ? ? ? ? {if $p == $i+1}
>
> ? ? ? ? ? ?
>
> ? ? ? ? ? ? ? ?
>
> ? ? ? ? ? ? ? ? {$i+1}
>
> ? ? ? ? ? ?
>
> ? ? ? ? {else/}
>
> ? ? ? ? ? ? {$i+1}
>
> ? ? ? ? {/if}
>
> ? ? {/for}
>
> ? ? =$count}layui-disabled{/if}">下一頁
>
>
## 九、模版分頁
* `paginate`內置了分頁實現,要給數據添加分頁輸出功能變得非常簡單
* `render`獲取翻頁html代碼
* `total`獲取總數量
controller代碼
> $select = Db::table('shop\_goods')->paginate(10);
>
> print\_r($select);echo '';
>
> foreach($select as $v){
>
> ? ? print\_r($v);echo '';
>
> }
>
> print\_r($select->render());echo '';
>
> print\_r('總數:'.$select->total());echo '';
>
> View::assign(\[
>
> ? ? 'select' => $select
>
> \]);
>
> return View::fetch();
view代碼
> {$select|raw}
css代碼
> .pagination {
>
> ? ? display: inline-block;
>
> ? ? padding-left: 0;
>
> ? ? margin: 20px 0;
>
> ? ? border-radius: 4px;
>
> }
>
> .pagination > li {
>
> ? ? display: inline;
>
> }
>
> .pagination > li > a,
>
> .pagination > li > span {
>
> ? ? position: relative;
>
> ? ? float: left;
>
> ? ? padding: 6px 12px;
>
> ? ? margin-left: -1px;
>
> ? ? line-height: 1.42857143;
>
> ? ? color: #337ab7;
>
> ? ? text-decoration: none;
>
> ? ? background-color: #fff;
>
> ? ? border: 1px solid #ddd;
>
> }
>
> .pagination > li:first-child > a,
>
> .pagination > li:first-child > span {
>
> ? ? margin-left: 0;
>
> ? ? border-top-left-radius: 4px;
>
> ? ? border-bottom-left-radius: 4px;
>
> }
>
> .pagination > li:last-child > a,
>
> .pagination > li:last-child > span {
>
> ? ? border-top-right-radius: 4px;
>
> ? ? border-bottom-right-radius: 4px;
>
> }
>
> .pagination > li > a:hover,
>
> .pagination > li > span:hover,
>
> .pagination > li > a:focus,
>
> .pagination > li > span:focus {
>
> ? ? z-index: 2;
>
> ? ? color: #23527c;
>
> ? ? background-color: #eee;
>
> ? ? border-color: #ddd;
>
> }
>
> .pagination > .active > a,
>
> .pagination > .active > span,
>
> .pagination > .active > a:hover,
>
> .pagination > .active > span:hover,
>
> .pagination > .active > a:focus,
>
> .pagination > .active > span:focus {
>
> ? ? z-index: 3;
>
> ? ? color: #fff;
>
> ? ? cursor: default;
>
> ? ? background-color: #337ab7;
>
> ? ? border-color: #337ab7;
>
> }
>
> .pagination > .disabled > span,
>
> .pagination > .disabled > span:hover,
>
> .pagination > .disabled > span:focus,
>
> .pagination > .disabled > a,
>
> .pagination > .disabled > a:hover,
>
> .pagination > .disabled > a:focus {
>
> ? ? color: #777;
>
> ? ? cursor: not-allowed;
>
> ? ? background-color: #fff;
>
> ? ? border-color: #ddd;
>
> }
## 十、模版分頁示例
參數描述list_rows 每頁數量page 當前頁pathurl路徑query url額外參數fragment url錨點var_page 分頁變量
controller代碼
> public function index(){
>
> ? ? $title = '商城';
>
> ? ? $login = '歐陽克';
>
> ? ? # 左側菜單
>
> ? ? $menu = Db::table('shop\_menu')->where('fid',0)->select();
>
> ? ? $left = $menu->toArray();
>
> ? ? foreach($left as &$left\_v){
>
> ? ? ? ? $left\_v\['lists'\] = Db::table('shop\_menu')->where('fid',$left\_v\['id'\])->select();
>
> ? ? }
>
> ? ? # 右側列表
>
> ? ? $param = Request::param();
>
> ? ? if(isset($param\['status'\]) && $param\['status'\] == 1){
>
> ? ? ? ? $where\['status'\] = 1;
>
> ? ? }else if(isset($param\['status'\]) && $param\['status'\] == 2){
>
> ? ? ? ? $where\['status'\] = 2;
>
> ? ? }else{
>
> ? ? ? ? $where = true;
>
> ? ? }
>
> ? ? $p = isset($param\['p'\]) ? $param\['p'\] : 1;
>
> ? ? # thinkphp 自帶分頁
>
> ? ? $list = Db::table('shop\_goods')
>
> ? ? ? ? ? ? ->where($where)
>
> ? ? ? ? ? ? ->order('add\_time DESC')
>
> ? ? ? ? ? ? ->order('id DESC')
>
> ? ? ? ? ? ? ->paginate(\[
>
> ? ? ? ? ? ? ? ? 'list\_rows'=> 10,
>
> ? ? ? ? ? ? ? ? 'query' => Request::param()
>
> ? ? ? ? ? ? \]);
>
> ? ? $right = $list->toArray();
>
> ? ? foreach($right as &$right\_v){
>
> ? ? ? ? $right\_v\['cat'\] = Db::table('shop\_cat')->where('id',$right\_v\['cat'\])->value('name');
>
> ? ? }
>
> ? ? View::assign(\[
>
> ? ? ? ? 'title'? => $title,
>
> ? ? ? ? 'login' => $login,
>
> ? ? ? ? 'left' => $left,
>
> ? ? ? ? 'right' => $right,
>
> ? ? ? ? 'list' => $list,
>
> ? ? ? ? 'status' => isset($param\['status'\]) ? $param\['status'\] : 0
>
> ? ? \]);
>
> ? ? return View::fetch();
>
> }
view代碼
> {$paginate|raw}
## 十一、SQL 調試
* `getLastSql`輸出上次執行的sql語句
* `getLastSql`方法只能獲取最后執行的 SQL 記錄
~~~
$select?=?Db::table('shop_goods')->select();
echo?Db::getLastSql();
~~~
* `fetchSql`方法直接返回當前的 SQL 而不執行
~~~
$select?=?Db::table('shop_goods')->fetchSql()->select();
echo?$select;
~~~
## 十二、動態配置數據庫
* config目錄database.php文件
> return \[
>
> ? ? 'connections' => \[
>
> ? ? ? ? 'ouyangke' => \[
>
> ? ? ? ? ? ? // 數據庫類型
>
> ? ? ? ? ? ? 'type'? ? ? ? ? ? ? => Env::get('database.type', 'mysql'),
>
> ? ? ? ? ? ? // 服務器地址
>
> ? ? ? ? ? ? 'hostname'? ? ? ? ? => Env::get('database.hostname', '127.0.0.1'),
>
> ? ? ? ? ? ? // 數據庫名
>
> ? ? ? ? ? ? 'database'? ? ? ? ? => 'ouyangke',
>
> ? ? ? ? ? ? // 用戶名
>
> ? ? ? ? ? ? 'username'? ? ? ? ? => Env::get('database.username', 'root'),
>
> ? ? ? ? ? ? // 密碼
>
> ? ? ? ? ? ? 'password'? ? ? ? ? => Env::get('database.password', 'root'),
>
> ? ? ? ? ? ? // 端口
>
> ? ? ? ? ? ? 'hostport'? ? ? ? ? => Env::get('database.hostport', '3306'),
>
> ? ? ? ? ? ? // 數據庫連接參數
>
> ? ? ? ? ? ? 'params'? ? ? ? ? ? => \[\],
>
> ? ? ? ? ? ? // 數據庫編碼默認采用utf8
>
> ? ? ? ? ? ? 'charset'? ? ? ? ? ?=> Env::get('database.charset', 'utf8'),
>
> ? ? ? ? ? ? // 數據庫表前綴
>
> ? ? ? ? ? ? 'prefix'? ? ? ? ? ? => Env::get('database.prefix', 'shop\_'),
>
> ? ? ? ? ? ? // 數據庫部署方式:0 集中式(單一服務器),1 分布式(主從服務器)
>
> ? ? ? ? ? ? 'deploy'? ? ? ? ? ? => 0,
>
> ? ? ? ? ? ? // 數據庫讀寫是否分離 主從式有效
>
> ? ? ? ? ? ? 'rw\_separate'? ? ? ?=> false,
>
> ? ? ? ? ? ? // 讀寫分離后 主服務器數量
>
> ? ? ? ? ? ? 'master\_num'? ? ? ? => 1,
>
> ? ? ? ? ? ? // 指定從服務器序號
>
> ? ? ? ? ? ? 'slave\_no'? ? ? ? ? => '',
>
> ? ? ? ? ? ? // 是否嚴格檢查字段是否存在
>
> ? ? ? ? ? ? 'fields\_strict'? ? ?=> true,
>
> ? ? ? ? ? ? // 是否需要斷線重連
>
> ? ? ? ? ? ? 'break\_reconnect'? ?=> false,
>
> ? ? ? ? ? ? // 監聽SQL
>
> ? ? ? ? ? ? 'trigger\_sql'? ? ? ?=> true,
>
> ? ? ? ? ? ? // 開啟字段緩存
>
> ? ? ? ? ? ? 'fields\_cache'? ? ? => false,
>
> ? ? ? ? ? ? // 字段緩存路徑
>
> ? ? ? ? ? ? 'schema\_cache\_path' => app()->getRuntimePath() . 'schema' . DIRECTORY\_SEPARATOR,
>
> ? ? ? ? \]
>
> ? ? \]
>
> \];
* ouyangke數據庫中的shop\_user表
> CREATE TABLE `shop\_user` (
>
> ? ? `uid` int(10) unsigned NOT NULL AUTO\_INCREMENT COMMENT '用戶ID',
>
> ? ? `account` varchar(50) NOT NULL COMMENT '賬戶',
>
> ? ? `password` char(32) NOT NULL COMMENT '密碼',
>
> ? ? `name` varchar(50) NOT NULL COMMENT '姓名',
>
> ? ? `status` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '狀態 1開啟 2關閉',
>
> ? ? `add\_time` int(10) unsigned NOT NULL COMMENT '添加時間',
>
> ? ? PRIMARY KEY (`uid`)
>
> ) ENGINE=MyISAM AUTO\_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT='后臺管理員';
* `connect`方法動態配置數據庫連接信息
> Db::connect('ouyangke')->table('shop\_user')->select();
`connect`方法必須在查詢的最開始調用,而且必須緊跟著調用查詢方法,否則可能會導致部分查詢失效或者依然使用默認的數據庫連接
## 十三、WHRER 鏈式操作(不常用)
* 和查詢表達式功能一樣,ThinkPHP 提供以下快捷查詢方法
連貫操作作用支持的參數類型whereOr*用于OR查詢字符串、數組和對象whereLike*模糊查詢字符串whereNotLike*模糊查詢字符串whereBetween*區間查詢字符串whereNotBetween*不在區間查詢 字符串whereIn*IN查詢字符串whereNotIn*不在IN查詢字符串whereNull*查詢字段是否是NULL字符串whereNotNull*查詢字段是否不是NULL字符串whereExists*EXISTS查詢字符串whereNotExists* 不在EXISTS查詢字符串whereBetweenTime*時間區間比較字符串whereTime*用于時間日期的快捷查詢字符串whereExp* 表達式查詢,支持SQL語法字符串whereFindInSet*FIND_IN_SET查詢字符串whereRaw*用于字符串條件直接查詢和操作字符串
## 十四、其他鏈式操作(不常用)
連貫操作作用 支持的參數類型alias 用于給當前數據表定義別名 字符串strict 用于設置是否嚴格檢測字段名是否存在 布爾值group 用于對查詢的group支持字符串having 用于對查詢的having支持字符串join*用于對查詢的join支持字符串和數組union*用于對查詢的union支持字符串、數組和對象distinct 用于查詢的distinct支持布爾值lock 用于數據庫的鎖機制布爾值cache 用于查詢緩存支持多個參數comment 用于SQL注釋字符串force 用于數據集的強制索引字符串partition 用于設置分區信息數組 字符串failException 用于設置沒有查詢到數據是否拋出異常布爾值sequence 用于設置自增序列名字符串replace 用于設置使用REPLACE方式寫入布爾值extra 用于設置額外查詢規則字符串duplicate 用于設置DUPLCATE信息數組 字符串procedure 用于設置當前查詢是否為存儲過程查詢布爾值master 用于設置主服務器讀取數據布爾值view*用于視圖查詢 字符串、數組
## 十五、事務操作
* `InnoDB`引擎支持事務處理,`MyISAM`不支持事務處理
~~~
//?啟動事務
Db::startTrans();
$data?=?['cat'=>'1','title'=>'日系小浪漫與溫暖羊毛針織拼接網紗百褶中長收腰連衣裙','price'=>'1598.35','add_time'=>1576080000];
$insert?=?Db::table('shop_goods')->insert($data);
if(empty($insert)){
????//?回滾事務
????Db::rollback();
}else{
????//?提交事務
????Db::commit();
}
~~~
* `transaction`方法操作數據庫事務,當閉包中的代碼發生異常會自動回滾
~~~
Db::transaction(function?()?{
????$data?=?['cat'=>'1','title'=>'日系小浪漫與溫暖羊毛針織拼接網紗百褶中長收腰連衣裙','price'=>'1598.35','add_time'=>1576080000];
????$insert?=?Db::table('shop_goods')->insert($data);
});
~~~
## 十六、數據集
* 數據庫通過select查詢,得到的數據集對象
* 返回的數據集對象是`think\Collection`,提供了和數組無差別用法,并且另外封裝了一些額外的方法
編號方法描述1 isEmpty是否為空2 toArray 轉換為數組3 all 所有數據4 merge 合并其它數據5 diff 比較數組,返回差集6 flip 交換數據中的鍵和值7 intersect 比較數組,返回交集8 keys 返回數據中的所有鍵名9 pop 刪除數據中的最后一個元素10 shift 刪除數據中的第一個元素11 unshift 在數據開頭插入一個元素12 push 在結尾插入一個元素13 reduce 通過使用用戶自定義函數,以字符串返回數組14 reverse 數據倒序重排15 chunk 數據分隔為多個數據塊16 each 給數據的每個元素執行回調17 filter 用回調函數過濾數據中的元素18 column 返回數據中的指定列19sort對數據排序20 order 指定字段排序21 shuffle 將數據打亂22 slice 截取數據中的一部分23 map用回調函數處理數組中的元素24 where 根據字段條件過濾數組中的元素25 whereLikeLike查詢過濾元素26 whereNotLike Not Like過濾元素27 whereIn IN查詢過濾數組中的元素28 whereNotIn Not IN查詢過濾數組中的元素29whereBetween Between查詢過濾數組中的元素30whereNotBetweenNot Between查詢過濾數組中的元素
~~~
$select?=?Db::table('shop_goods')
????????????->field('title,price,id')
????????????->where('status',1)
????????????->order('price','DESC')
????????????->select();
if($select->isEmpty()){
????echo?'未查詢到數據';
}else{
????print_r($select->toArray());
}
~~~
備:在模型中進行數據集查詢,全部返回數據集對象,但使用的是`think\model\Collection類`(繼承think\\Collection),但用法是一致的。