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

模型的增刪改查

Original 2019-03-15 23:12:46 315
abstrakt:<?php //模型 namespace app\index\model; use think\Model; use think\model\concern\SoftDelete;//導(dǎo)入軟刪除  他是trait方法集 class Staff extends Model {    &
<?php
//模型
namespace app\index\model;
use think\Model;
use think\model\concern\SoftDelete;//導(dǎo)入軟刪除  他是trait方法集
class Staff extends Model
{
    use SoftDelete;//相當(dāng)于把trait類(lèi)里面的代碼復(fù)制到這里
    //設(shè)置數(shù)據(jù)表名稱(chēng)       直接與要操作的數(shù)據(jù)表綁定
    protected $table = 'user';
    //設(shè)置主鍵:默認(rèn)是id,不是的話就要進(jìn)行設(shè)置
    protected $pk = 'id';
    //設(shè)置刪除時(shí)間的字段名
    protected $deleteTime = 'delete_time';//這個(gè)會(huì)在數(shù)據(jù)表中創(chuàng)建一個(gè)字段來(lái)保存刪除的時(shí)間戳
    //設(shè)置軟刪除字段的默認(rèn)值
    protected $defaultSoftDelete = 0;
}
//操作
<?php
namespace app\index\controller;
use think\Controller;
use app\index\model\Staff as StaffModel;
use think\Db;
use think\model\concern\SoftDelete;//設(shè)置模型類(lèi)別名

class Staff extends Controller
{
    //實(shí)例化模型  new StaffModel()
    public function instance(StaffModel $staff)//這里用依賴(lài)注入   只要有類(lèi)型約束,在外部系統(tǒng)就會(huì)自動(dòng)實(shí)例化對(duì)象
    {
         dump($staff->getName());//查看模型名稱(chēng)
        //新增一條記錄
        $staff->name = '劉大嬸';
        $staff->age =36;
        $staff->phone=3243434455;
        //新增操作
        $staff->save();
        return '新增成功,id='.$staff->id;
    }
    //模型查詢(xún)
    public function query()
    {
        //單條記錄:get(主鍵/閉包)
        //閉包就是匿名回調(diào)函數(shù),將函數(shù)作為參數(shù)進(jìn)行傳遞
        //$sta = StaffModel::get(17);//查詢(xún)可以返回一個(gè)對(duì)象
        //dump($sta);
        //echo $sta['name'],'<br/>';
        //echo $sta->age,'<br/>';

        //用閉包來(lái)創(chuàng)建查詢(xún)條件
        $sta = StaffModel::get(function ($query){//$query必傳,這是系統(tǒng)自動(dòng)實(shí)例化過(guò)的對(duì)象
            $query->where('age','>','50')->where('sex',0);
        });
        dump($sta);
        //也可以直接用靜態(tài)調(diào)用Db類(lèi)的查詢(xún)構(gòu)造器進(jìn)行查詢(xún)
        //StaffModel  == Db::table('user')   在模型里面綁定了數(shù)據(jù)表的,所以這兩種是相等的
        //第一個(gè)方法要靜態(tài)調(diào)用,后面跟查詢(xún)構(gòu)造器一樣寫(xiě)
        //$staff = StaffModel::where('age','<','50')->find();
        //dump($staff);
        //多條記錄查詢(xún):all(主鍵列表\閉包)
        //返回值是多個(gè)數(shù)組/對(duì)象數(shù)組
        //$staff = StaffModel::all();
        //dump($staff);

        //用閉包來(lái)查詢(xún)
        /*$staff = StaffModel::all(function ($query){
            $query->where('age','>','60');
        });*/
        /*dump($staff);*/
        echo '<hr>';
        //采用閉包來(lái)實(shí)現(xiàn)將請(qǐng)求變量注入到閉包條件中   來(lái)自u(píng)rl地址中的參數(shù)
        //$this->request ===new \think\facade\Request  請(qǐng)求對(duì)象來(lái)自controller控制器中,可以獲取請(qǐng)求的變量
        $age = $this->request->param('age') ? : 40;
        $staff = StaffModel::all(function ($query) use ($age) {//閉包中引入變量
            $query->where('age','>',$age);
        });
        dump($staff);
    }
    //更新操作
    public function update()
    {
        //先查詢(xún)?cè)谫x值最后保存
       /* $staff = StaffModel::get(11);
        $staff->phone = 11111111;//更新記錄字段
        $staff->save();//將更新寫(xiě)回表中*/

       //靜態(tài)方法更新
        StaffModel::update(
            ['phone'=>22222222],//要更新的字段
            ['id'=>11]//條件
        );
        //復(fù)雜更新,當(dāng)條件比較多時(shí),就要用閉包了
       /* StaffModel::update(
          ['age'=>\think\Db::raw('age+10')],
          function ($querty){
              $querty->where('age','>','60');
          }
        );*/
        //查詢(xún)構(gòu)造器來(lái)更新數(shù)據(jù)
        StaffModel::where('age','>','60')
            ->data(['age'=>\think\Db::raw('age-10')])
            ->update();

    }
    //新增數(shù)據(jù)
    public function create()
    {
        //1.save()   2.create(數(shù)據(jù),字段)
        $data = ['name'=>'流星','age'=>90,'phone'=>9999999];
        //$field = ['name','age'];//允許插入的字段
        //StaffModel::create($data,$field);
        //用查詢(xún)構(gòu)造器來(lái)插入數(shù)據(jù)
        StaffModel::insert($data);
    }
    //模型的刪除
    public function delete()
    {
        //destroy(主鍵/主鍵列表/閉包)
       // StaffModel::destroy([21,22,23]);

        //刪除條件推薦使用閉包
        /*StaffModel::destroy(function ($query){
            $query->where('id','>',10);
        });*/

        //查詢(xún)構(gòu)造器實(shí)現(xiàn)刪除
        StaffModel::where('id',17)
            ->delete();
        //工作上一般用軟刪除

    }
    //軟刪除   必須要在模型里面進(jìn)行配置
    public function softDelete()
    {
        //StaffModel::destroy(19);
        //軟刪除的數(shù)據(jù)在普通查詢(xún)中不可見(jiàn)

        //如果想在查詢(xún)的時(shí)候看到已經(jīng)被刪除的記錄
        $res = StaffModel::withTrashed()
            ->where('id','>',11)
            ->select();
        dump($res);
        //只看被軟刪除的數(shù)據(jù)
        $res = StaffModel::onlyTrashed()->select();
        dump($res);
    }

}


Korrigierender Lehrer:查無(wú)此人Korrekturzeit:2019-03-16 09:41:23
Zusammenfassung des Lehrers:完成的不錯(cuò)。增刪查改是對(duì)數(shù)據(jù)庫(kù)的基本操作,要多練習(xí),刪除慎用。繼續(xù)加油。

Versionshinweise

Beliebte Eintr?ge