这次练习主要是模型的增删改查,教程中教了几种方法,我觉得应该要把其中一种学熟,而老师推荐用闭包的方法,所以这次练习,主要是闭包为主。另外也试了一下各种参数
<?php
namespace app\index\controller;
use think\Controller;
use app\index\model\cate as cateM;
use think\Db;
use think\request;
class Cate extends Controller
{
//闭包查询
public function select()
{
//单调查询
$cate = cateM::get(function($suibian){
$suibian->field(['catename','pid'])->where('type',2)
->where('rec_index',1);
});
dump($cate);
//多条查询
$type = $this->request->param('type')?:5;
$cate2 = cateM::all(function($suibian)use($type){
$suibian->where(['type'=>$type]);
});
dump($cate2);
}
public function update()
{
//使用闭包更新
$cate = cateM::update(['type'=>5,'rec_index'=>3],function($suibian){
$suibian->where('type','<',3);
},'type');
//使用闭包数据加减
$cate = cateM::update(['type'=>Db::raw('type+50'),'rec_index'=>3],function($suibian){
$suibian->where('type','>',6);
},'type');
}
public function add()
{
$data=[
'catename'=>'山地单车',
'type'=>'2',
'keywords'=>'爬坡'
];
$field = ['type','catename'];
$cate = cateM::create($data,$field);
}
public function delete()
{
$cate = cateM::destroy(function($suibian){
$suibian->where('type',3);
});
}
public function softDelete()
{
cateM::destroy(49);
}
}