亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

ThinkPHP6.0 Rapid Development Manual (Case Version) / ThinkPHP6.0 數(shù)據(jù)庫(kù)鏈?zhǔn)讲僮?

ThinkPHP6.0 數(shù)據(jù)庫(kù)鏈?zhǔn)讲僮?/h1>

數(shù)據(jù)庫(kù)提供的鏈?zhǔn)讲僮鞣椒?,可以有效的提高?shù)據(jù)存取的代碼清晰度和開(kāi)發(fā)效率,并且支持所有的CURD操作。



ThinkPHP6 數(shù)據(jù)庫(kù)鏈?zhǔn)讲僮?/h2>
  • 數(shù)據(jù)庫(kù)提供的鏈?zhǔn)讲僮鞣椒ǎ梢杂行У奶岣邤?shù)據(jù)存取的代碼清晰度和開(kāi)發(fā)效率,并且支持所有的CURD操作

  • 帶*標(biāo)識(shí)的表示支持多次調(diào)用

連貫操作 作用 支持的參數(shù)類(lèi)型
where*用于AND查詢(xún)字符串、數(shù)組和對(duì)象
table 用于定義要操作的數(shù)據(jù)表名稱(chēng)字符串和數(shù)組
name 用于定義要操作的數(shù)據(jù)表名稱(chēng)字符串
field*用于定義要查詢(xún)的字段(支持字段排除)字符串和數(shù)組
order*用于對(duì)結(jié)果排序字符串和數(shù)組
limit 用于限制查詢(xún)結(jié)果數(shù)量字符串和數(shù)字
page 用于查詢(xún)分頁(yè)(內(nèi)部會(huì)轉(zhuǎn)換成limit)字符串和數(shù)字

一、表達(dá)式查詢(xún)

  • 表達(dá)式是SQL語(yǔ)句的條件

  • 表達(dá)式不分大小寫(xiě)

  • 表達(dá)式寫(xiě)在where里

表達(dá)式含義查詢(xún)方法
=等于
<>不等于
>大于
>=大于等于
<小于
<=小于等于
[NOT] LIKE模糊查詢(xún)whereLike/whereNotLike
[NOT] BETWEEN(不在)區(qū)間查詢(xún)whereBetween/whereNotBetween
[NOT] IN(不在)IN 查詢(xún) whereIn/whereNotIn
[NOT] NULL查詢(xún)字段是否(不)是NULLwhereNull/whereNotNull

where查詢(xún)

  • where方法在鏈?zhǔn)讲僮鞣椒ɡ锩媸亲畛S玫姆椒ǎ梢酝瓿砂ㄆ胀ú樵?xún)、表達(dá)式查詢(xún)、快捷查詢(xún)、區(qū)間查詢(xún)、組合查詢(xún)?cè)趦?nèi)的條件查詢(xún)操作

# 等于(=)

$select = Db::table('shop_goods')->where('id','=','1')->select();

print_r($select->toArray());


# 不等于(<>)

$select = Db::table('shop_goods')->where('id','<>','2')->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','<','5')->select();

print_r($select->toArray());


# 小于等于(<=)

$select = Db::table('shop_goods')->where('id','<=','6')->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());

二、數(shù)據(jù)表

1、table 和 name

# 必須完整數(shù)據(jù)庫(kù)名

$select = Db::table('shop_goods')->where('id','10')->select();

print_r($select->toArray());

# 數(shù)據(jù)庫(kù)未設(shè)置前綴

$select = Db::name('shop_goods')->where('id','11')->select();

print_r($select->toArray());

# 數(shù)據(jù)庫(kù)設(shè)置前綴,無(wú)前綴訪問(wèn)

$select = Db::name('list')->where('id','12')->select();

print_r($select->toArray());

2、數(shù)據(jù)庫(kù)前綴

數(shù)據(jù)庫(kù)配置 database.php

return [

    'connections'     => [

        'mysql' => [

            // 數(shù)據(jù)庫(kù)表前綴

            'prefix'  => Env::get('database.prefix', 'shop_'),

        ]

    ]

];

三、返回值

1、field 

  • field 方法主要作用是標(biāo)識(shí)要返回或者操作的字段,可以用于查詢(xún)和寫(xiě)入操作

  • 所有的查詢(xún)方法都可以使用field方法

# 字符串

$select = Db::table('shop_goods')

            ->field('title,price,discount as d')

            ->where('status',1)

            ->select();

print_r($select->toArray());


# 數(shù)組

$select = Db::table('shop_goods')

            ->field([

                'title',

                'price',

                'discount'=>'d'

            ])

            ->where('status',1)

            ->select();

print_r($select->toArray());


# 添加,只能添加這幾個(gè)字段

# 多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);


# 查詢(xún)?nèi)孔侄?,速度較快

$select = Db::table('shop_goods')

            ->field(true)

            // ->field('*')

            ->where('status',1)

            ->select();

print_r($select->toArray());

2、withoutField

  • withoutField 方法作用 排除數(shù)據(jù)表中的字段

Db::table('shop_goods')->withoutField('id')->select();

3、fieldRaw

  • fieldRaw 方法直接使用mysql函數(shù)

Db::table('shop_goods')->fieldRaw('id,sum(price)')->select();

四、排序

1、order 方法用于對(duì)操作的結(jié)果排序或者優(yōu)先級(jí)限制

  • 默認(rèn)正序

  • 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函數(shù)

$select = Db::table('shop_goods')

            ->field('title,price,id')

            ->where('status',1)

            ->orderRaw("field(title,'price','discount','stock')")

            ->select();

print_r($select->toArray());

五、分頁(yè)

  • limit 方法主要用于指定查詢(xún)和操作的數(shù)量

$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 方法主要用于分頁(yè)查詢(xún)

$select = Db::table('shop_goods')

            ->field('title,price,id')

            ->where('status',1)

            ->order('price','DESC')

            ->page(1,5)

            ->select();

print_r($select->toArray());

六、聚合查詢(xún)

  • 聚合方法如果沒(méi)有數(shù)據(jù),默認(rèn)都是0,聚合查詢(xún)都可以配合其它查詢(xún)條件

方法功能
count 統(tǒng)計(jì)數(shù)量,參數(shù)是要統(tǒng)計(jì)的字段名(可選)
max 獲取最大值,參數(shù)是要統(tǒng)計(jì)的字段名(必須)
min 獲取最小值,參數(shù)是要統(tǒng)計(jì)的字段名(必須)
avg 獲取平均值,參數(shù)是要統(tǒng)計(jì)的字段名(必須)
sum獲取總數(shù),參數(shù)是要統(tǒng)計(jì)的字段名(必須)

// 統(tǒng)計(jì)數(shù)量,參數(shù)是要統(tǒng)計(jì)的字段名(可選)

$select = Db::table('shop_goods')->count();

print_r($select);


// 獲取最大值,參數(shù)是要統(tǒng)計(jì)的字段名(必須)

$select = Db::table('shop_goods')->max('id');

print_r($select);


// 獲取最小值,參數(shù)是要統(tǒng)計(jì)的字段名(必須)

$select = Db::table('shop_goods')->min('id');

print_r($select);


// 獲取平均值,參數(shù)是要統(tǒng)計(jì)的字段名(必須)

$select = Db::table('shop_goods')->avg('id');

print_r($select);


// 獲取總數(shù),參數(shù)是要統(tǒng)計(jì)的字段名(必須)

$select = Db::table('shop_goods')->sum('id');

print_r($select);

七、搜索、排序示例

controller代碼

public function index(){

    $title = '商城';

    $login = '歐陽(yáng)克';

    # 左側(cè)菜單

    $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();

    }

    # 右側(cè)列表

    $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代碼

<form class="layui-form" method="post">

    <div class="layui-form-item" style="margin-top:10px;">

        <div class="layui-input-inline">

            <select name="status">

                <option value="0" {if $status==0}selected{/if}>全部</option>

                <option value="1" {if $status==1}selected{/if}>開(kāi)啟</option>

                <option value="2" {if $status==2}selected{/if}>關(guān)閉</option>

            </select>

        </div>

        <button class="layui-btn layui-btn-primary"><i class="layui-icon">&#xe615;</i>搜索</button>

    </div>

</form>

八、分頁(yè)示例

controller代碼

public function index(){

    $title = '商城';

    $login = '歐陽(yáng)克';

    # 左側(cè)菜單

    $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();

    }

    # 右側(cè)列表

    $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;

    // 統(tǒng)計(jì)總數(shù)

    $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代碼

<div class="layui-box layui-laypage layui-laypage-default">

    <a href="/index.php/Index/index?p={$p-1}&status={$status}" class="layui-laypage-prev {if $p<=1}layui-disabled{/if}">上一頁(yè)</a>

    {for start="0" end="$count"}

        {if $p == $i+1}

            <span class="layui-laypage-curr">

                <em class="layui-laypage-em"></em>

                <em>{$i+1}</em>

            </span>

        {else/}

            <a href="/index.php/Index/index?p={$i+1}&status={$status}">{$i+1}</a>

        {/if}

    {/for}

    <a href="/index.php/Index/index?p={$p+1}&status={$status}" class="layui-laypage-next {if $p>=$count}layui-disabled{/if}">下一頁(yè)</a>

</div>

九、模版分頁(yè)

  • paginate 內(nèi)置了分頁(yè)實(shí)現(xiàn),要給數(shù)據(jù)添加分頁(yè)輸出功能變得非常簡(jiǎn)單

  • render 獲取翻頁(yè)html代碼

  • total 獲取總數(shù)量

controller代碼

$select = Db::table('shop_goods')->paginate(10);

print_r($select);echo '<hr>';

foreach($select as $v){

    print_r($v);echo '<hr>';

}

print_r($select->render());echo '<hr>';

print_r('總數(shù):'.$select->total());echo '<hr>';

View::assign([

    'select' => $select

]);

return View::fetch();

view代碼

<div>{$select|raw}</div>

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;

}

十、模版分頁(yè)示例

參數(shù)描述
list_rows 每頁(yè)數(shù)量
page 當(dāng)前頁(yè)
pathurl路徑
query url額外參數(shù)
fragment url錨點(diǎn)
var_page 分頁(yè)變量

controller代碼

public function index(){

    $title = '商城';

    $login = '歐陽(yáng)克';

    # 左側(cè)菜單

    $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();

    }

    # 右側(cè)列表

    $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 自帶分頁(yè)

    $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代碼

<div>{$paginate|raw}</div>

十一、SQL 調(diào)試

  • getLastSql 輸出上次執(zhí)行的sql語(yǔ)句

  • getLastSql 方法只能獲取最后執(zhí)行的 SQL 記錄

$select = Db::table('shop_goods')->select();
echo Db::getLastSql();
  • fetchSql 方法直接返回當(dāng)前的 SQL 而不執(zhí)行

$select = Db::table('shop_goods')->fetchSql()->select();
echo $select;

十二、動(dòng)態(tài)配置數(shù)據(jù)庫(kù)

  • config目錄database.php文件

return [

    'connections' => [

        'ouyangke' => [

            // 數(shù)據(jù)庫(kù)類(lèi)型

            'type'              => Env::get('database.type', 'mysql'),

            // 服務(wù)器地址

            'hostname'          => Env::get('database.hostname', '127.0.0.1'),

            // 數(shù)據(jù)庫(kù)名

            'database'          => 'ouyangke',

            // 用戶(hù)名

            'username'          => Env::get('database.username', 'root'),

            // 密碼

            'password'          => Env::get('database.password', 'root'),

            // 端口

            'hostport'          => Env::get('database.hostport', '3306'),

            // 數(shù)據(jù)庫(kù)連接參數(shù)

            'params'            => [],

            // 數(shù)據(jù)庫(kù)編碼默認(rèn)采用utf8

            'charset'           => Env::get('database.charset', 'utf8'),

            // 數(shù)據(jù)庫(kù)表前綴

            'prefix'            => Env::get('database.prefix', 'shop_'),

            // 數(shù)據(jù)庫(kù)部署方式:0 集中式(單一服務(wù)器),1 分布式(主從服務(wù)器)

            'deploy'            => 0,

            // 數(shù)據(jù)庫(kù)讀寫(xiě)是否分離 主從式有效

            'rw_separate'       => false,

            // 讀寫(xiě)分離后 主服務(wù)器數(shù)量

            'master_num'        => 1,

            // 指定從服務(wù)器序號(hào)

            'slave_no'          => '',

            // 是否嚴(yán)格檢查字段是否存在

            'fields_strict'     => true,

            // 是否需要斷線(xiàn)重連

            'break_reconnect'   => false,

            // 監(jiān)聽(tīng)SQL

            'trigger_sql'       => true,

            // 開(kāi)啟字段緩存

            'fields_cache'      => false,

            // 字段緩存路徑

            'schema_cache_path' => app()->getRuntimePath() . 'schema' . DIRECTORY_SEPARATOR,

        ]

    ]

];

  • ouyangke數(shù)據(jù)庫(kù)中的shop_user表

CREATE TABLE `shop_user` (

    `uid` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '用戶(hù)ID',

    `account` varchar(50) NOT NULL COMMENT '賬戶(hù)',

    `password` char(32) NOT NULL COMMENT '密碼',

    `name` varchar(50) NOT NULL COMMENT '姓名',

    `status` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '狀態(tài) 1開(kāi)啟 2關(guān)閉',

    `add_time` int(10) unsigned NOT NULL COMMENT '添加時(shí)間',

    PRIMARY KEY (`uid`)

) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT='后臺(tái)管理員';

  • connect 方法動(dòng)態(tài)配置數(shù)據(jù)庫(kù)連接信息

Db::connect('ouyangke')->table('shop_user')->select();

connect 方法必須在查詢(xún)的最開(kāi)始調(diào)用,而且必須緊跟著調(diào)用查詢(xún)方法,否則可能會(huì)導(dǎo)致部分查詢(xún)失效或者依然使用默認(rèn)的數(shù)據(jù)庫(kù)連接

十三、WHRER 鏈?zhǔn)讲僮?不常用)

  • 和查詢(xún)表達(dá)式功能一樣,ThinkPHP 提供以下快捷查詢(xún)方法

連貫操作作用支持的參數(shù)類(lèi)型
whereOr*用于OR查詢(xún)字符串、數(shù)組和對(duì)象
whereLike*模糊查詢(xún)字符串
whereNotLike*模糊查詢(xún)字符串
whereBetween*區(qū)間查詢(xún)字符串
whereNotBetween*不在區(qū)間查詢(xún) 字符串
whereIn*IN查詢(xún)字符串
whereNotIn*不在IN查詢(xún)字符串
whereNull*查詢(xún)字段是否是NULL字符串
whereNotNull*查詢(xún)字段是否不是NULL字符串
whereExists*EXISTS查詢(xún)字符串
whereNotExists* 不在EXISTS查詢(xún)字符串
whereBetweenTime*時(shí)間區(qū)間比較字符串
whereTime*用于時(shí)間日期的快捷查詢(xún)字符串
whereExp* 表達(dá)式查詢(xún),支持SQL語(yǔ)法字符串
whereFindInSet*FIND_IN_SET查詢(xún)字符串
whereRaw*用于字符串條件直接查詢(xún)和操作字符串

十四、其他鏈?zhǔn)讲僮?不常用)

連貫操作作用 支持的參數(shù)類(lèi)型
alias 用于給當(dāng)前數(shù)據(jù)表定義別名 字符串
strict 用于設(shè)置是否嚴(yán)格檢測(cè)字段名是否存在 布爾值
group 用于對(duì)查詢(xún)的group支持字符串
having 用于對(duì)查詢(xún)的having支持字符串
join*用于對(duì)查詢(xún)的join支持字符串和數(shù)組
union*用于對(duì)查詢(xún)的union支持字符串、數(shù)組和對(duì)象
distinct 用于查詢(xún)的distinct支持布爾值
lock 用于數(shù)據(jù)庫(kù)的鎖機(jī)制布爾值
cache 用于查詢(xún)緩存支持多個(gè)參數(shù)
comment 用于SQL注釋字符串
force 用于數(shù)據(jù)集的強(qiáng)制索引字符串
partition 用于設(shè)置分區(qū)信息數(shù)組 字符串
failException 用于設(shè)置沒(méi)有查詢(xún)到數(shù)據(jù)是否拋出異常布爾值
sequence 用于設(shè)置自增序列名字符串
replace 用于設(shè)置使用REPLACE方式寫(xiě)入布爾值
extra 用于設(shè)置額外查詢(xún)規(guī)則字符串
duplicate 用于設(shè)置DUPLCATE信息數(shù)組 字符串
procedure 用于設(shè)置當(dāng)前查詢(xún)是否為存儲(chǔ)過(guò)程查詢(xún)布爾值
master 用于設(shè)置主服務(wù)器讀取數(shù)據(jù)布爾值
view*用于視圖查詢(xún) 字符串、數(shù)組

十五、事務(wù)操作

  • InnoDB引擎支持事務(wù)處理,MyISAM不支持事務(wù)處理

// 啟動(dòng)事務(wù)
Db::startTrans();
$data = ['cat'=>'1','title'=>'日系小浪漫與溫暖羊毛針織拼接網(wǎng)紗百褶中長(zhǎng)收腰連衣裙','price'=>'1598.35','add_time'=>1576080000];
$insert = Db::table('shop_goods')->insert($data);
if(empty($insert)){
    // 回滾事務(wù)
    Db::rollback();
}else{
    // 提交事務(wù)
    Db::commit();
}
  • transaction 方法操作數(shù)據(jù)庫(kù)事務(wù),當(dāng)閉包中的代碼發(fā)生異常會(huì)自動(dòng)回滾

Db::transaction(function () {
    $data = ['cat'=>'1','title'=>'日系小浪漫與溫暖羊毛針織拼接網(wǎng)紗百褶中長(zhǎng)收腰連衣裙','price'=>'1598.35','add_time'=>1576080000];
    $insert = Db::table('shop_goods')->insert($data);
});

十六、數(shù)據(jù)集

  • 數(shù)據(jù)庫(kù)通過(guò)select查詢(xún),得到的數(shù)據(jù)集對(duì)象

  • 返回的數(shù)據(jù)集對(duì)象是think\Collection,提供了和數(shù)組無(wú)差別用法,并且另外封裝了一些額外的方法

編號(hào)方法描述
isEmpty是否為空
toArray 轉(zhuǎn)換為數(shù)組
all 所有數(shù)據(jù)
merge 合并其它數(shù)據(jù)
diff 比較數(shù)組,返回差集
flip 交換數(shù)據(jù)中的鍵和值
intersect 比較數(shù)組,返回交集
keys 返回?cái)?shù)據(jù)中的所有鍵名
pop 刪除數(shù)據(jù)中的最后一個(gè)元素
10 shift 刪除數(shù)據(jù)中的第一個(gè)元素
11 unshift 在數(shù)據(jù)開(kāi)頭插入一個(gè)元素
12 push 在結(jié)尾插入一個(gè)元素
13 reduce 通過(guò)使用用戶(hù)自定義函數(shù),以字符串返回?cái)?shù)組
14 reverse 數(shù)據(jù)倒序重排
15 chunk 數(shù)據(jù)分隔為多個(gè)數(shù)據(jù)塊
16 each 給數(shù)據(jù)的每個(gè)元素執(zhí)行回調(diào)
17 filter 用回調(diào)函數(shù)過(guò)濾數(shù)據(jù)中的元素
18 column 返回?cái)?shù)據(jù)中的指定列
19sort對(duì)數(shù)據(jù)排序
20 order 指定字段排序
21 shuffle 將數(shù)據(jù)打亂
22 slice 截取數(shù)據(jù)中的一部分
23 map用回調(diào)函數(shù)處理數(shù)組中的元素
24 where 根據(jù)字段條件過(guò)濾數(shù)組中的元素
25 whereLikeLike查詢(xún)過(guò)濾元素
26 whereNotLike Not Like過(guò)濾元素
27 whereIn IN查詢(xún)過(guò)濾數(shù)組中的元素
28 whereNotIn Not IN查詢(xún)過(guò)濾數(shù)組中的元素
29whereBetween Between查詢(xún)過(guò)濾數(shù)組中的元素
30whereNotBetweenNot Between查詢(xún)過(guò)濾數(shù)組中的元素
$select = Db::table('shop_goods')
            ->field('title,price,id')
            ->where('status',1)
            ->order('price','DESC')
            ->select();
if($select->isEmpty()){
    echo '未查詢(xún)到數(shù)據(jù)';
}else{
    print_r($select->toArray());
}

備:在模型中進(jìn)行數(shù)據(jù)集查詢(xún),全部返回?cái)?shù)據(jù)集對(duì)象,但使用的是think\model\Collection類(lèi)(繼承think\Collection),但用法是一致的。