本篇內(nèi)容介紹了“TP5框架model怎么增刪改查、聚合、時(shí)間戳、軟刪除”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

使用model 查詢數(shù)據(jù),添加數(shù)據(jù),修改數(shù)據(jù),刪除數(shù)據(jù)
聚合操作
獲取器,修改器
自動(dòng)添加時(shí)間戳(創(chuàng)建時(shí)間,修改時(shí)間)
軟刪除
1、使用model查詢數(shù)據(jù)
$res = User::get(1); //獲取主鍵為1的數(shù)據(jù),得到的是一個(gè)對(duì)象 $res = $res->toArray(); //將對(duì)象轉(zhuǎn)化為數(shù)組 dump($res->name); //獲取 $res 里 name 字段的值
//使用閉包函數(shù)查詢 id=1 的記錄
$res = User::get(function($query){
$query->where("id","eq",1)
->field('name')
});$res = User::where("id",10)->value('name');
$res = User::where("id",10)->field('name')->find();
$res = User::column('email'); //查詢所有的 email 字段值
$res = User::where("id",">",5)->select(); //查詢所有id大于5的記錄$res = User::all('1,2'); //查詢主鍵等于 1 或2 的記錄
foreach($res as $val) //轉(zhuǎn)化為數(shù)組
{
dump($val->toArray());
}//使用閉包函數(shù)查詢 id<5 的記錄
$res = User::get(function($query){
$query->where("id","<",5)
->field('name')
});2、使用model添加數(shù)據(jù)
$res = User::create([ 'name' => 'yulong', 'pwd' => '123' ],true); //第二個(gè)參數(shù)為true時(shí),只添加數(shù)據(jù)表中已有的字段,不報(bào)錯(cuò),不寫則默認(rèn)為false;;;true 也可以換成一個(gè)數(shù)組,數(shù)組里存放數(shù)據(jù)表中的字段,表示僅允許數(shù)組中的字段添加數(shù)據(jù) $res->id; //本次添加的自增id dump($res);
$usermodel = new User; $res = $usermodel ->allowField(true) //僅允許添加數(shù)據(jù)表中存在的字段,也可以寫成數(shù)組 ->save([ 'name' => 'yulong', 'pwd' => '123' ]); dump($res->id); //獲取新添加數(shù)據(jù)的自增id
$usermodel = new User; $res = $usermodel->saveAll([ //一次保存多條數(shù)據(jù) 'name' => 'yulong001', 'name' => 'yulong002' ]); dump($ers);
3、使用model更新數(shù)據(jù)
$res = User::update([
'name' => 'yulong002'
],['id'=>1]); //更新 id=1 的記錄
$res = User::update([
'name' => 'yulong002'
],function(){
$query->where("id","LT",5); //使用閉包函數(shù)更新 id<5 的記錄
});
dump($res);$res = User::where("id","<",6) //返回值是被更新數(shù)據(jù)的行數(shù)
->update([
'name' => 'hahahaha'
]);4、使用model刪除數(shù)據(jù)
$res = User::destriy(1); //刪除主鍵為1的記錄,返回影響數(shù)據(jù)的行數(shù),也可以傳遞數(shù)組
$usermodel = User::get(1);
$res = $usermodel->delete();
$res = User::where("id",5)->delete(); // where() 里面有三個(gè)參數(shù), 字段值,條件,數(shù)值
dump($res);5、使用model聚合操作
$res = User::where("id",">",5)->count(); //查詢id大于5的記錄條數(shù)
// max 可以換成其他的 如 min / sum / avg
$res = User::max('num'); //查詢 num 字段中的較大值
$res = User::where("id","<",5)->max('num'); //id<5 的記錄中的 num 較大值6、使用模型獲取器
//model
//方法名: get字段名Attr
//controller中獲取原始數(shù)據(jù)使用 $res->getData()
public function getSexSttr($val){
switch($val){
case '1':
return "男";
break;
case '2';
return '女';
break;
default:
return '未知';
break;
}
}7、使用模型修改器
//model 修改器命名 set字段名Attr
//修改器作用:在往數(shù)據(jù)庫添加字段時(shí),控制器中寫未處理的數(shù)據(jù),在模型中的修改器中寫處理數(shù)據(jù)的方法,這樣添加到數(shù)據(jù)庫中的數(shù)據(jù)就是處理過得數(shù)據(jù)了
public function setPwdAttr($val){
return md5($val);
}
// $val代表 pwd 字段,$data代表接收到的所有數(shù)據(jù) ,返回的值就是 pwd+email
public function setPwdAttr($val,$data){
return $val.$data['email'];
}8、自動(dòng)往數(shù)據(jù)庫中添加時(shí)間戳
//自動(dòng)往 time 字段中加入時(shí)間戳
public function setTimeAttr(){
return time();
}
//在數(shù)據(jù)添加時(shí)發(fā)生改變
protected $insert = [ 'time_insert' ]; //設(shè)置字段
public function setTimeInsertAttr(){ //將字段值設(shè)置為當(dāng)前時(shí)間
return time();
}
//在更新數(shù)據(jù)時(shí)發(fā)生改變
protected $update = [ 'time_update' ]; //設(shè)置字段
public function setTimeUpdateAttr(){ //將字段值設(shè)置為當(dāng)前時(shí)間
return time();
}9、model時(shí)間戳
// 數(shù)據(jù)庫中的字段 create_time update_time // database.php 中更改配置 'auto_timeStamp' => true // 不推薦使用此方法,因?yàn)槿绻愕臄?shù)據(jù)庫表中沒有 對(duì)應(yīng)的字段 ,程序可能就會(huì)報(bào)錯(cuò) // 可以單獨(dú)在 某個(gè)模型中 添加屬性 protected $autoWriteTimeStamp = true; //開啟自動(dòng)加入時(shí)間戳 protected $createTime = 'create_at'; //設(shè)置 創(chuàng)建的時(shí)候?qū)懭?nbsp;的字段 ,值可以為false,關(guān)閉操作 protedted $updateTime = 'update_at'; //設(shè)置 創(chuàng)建和更新的時(shí)候?qū)懭?nbsp;的字段 ,值可以為false,關(guān)閉操作
10、軟刪除
// model
// 數(shù)據(jù)表中的字段 delete_time,默認(rèn)值可以為 null
use traits\model\SoftDelete; //使用軟刪除的類
class User extends Model
{
use SoftDelete; //在類的開頭 use SoftDelete;
protected $deleteTime = 'delete_at'; //設(shè)置軟刪除的字段,默認(rèn)為 delete_time
}
$res = User::destroy(3,true); //刪除主鍵為3的記錄,第二個(gè)參數(shù)為 true 時(shí),不是軟刪除,是tm真刪了
$ress = User::get(4);
$res = $ress->delete(true); // delete() 沒值時(shí),為軟刪除;值為true,tm的真刪
// controller 獲取到 軟刪除 的記錄
$res = User::withTrashed(true)->find(1); //得到id為1 的經(jīng)過軟刪除 刪除的記錄
dump($res->getData()); //獲取原始數(shù)據(jù)
$res = User::onlyTrashed()->select(); //獲取所有軟刪除的數(shù)據(jù)“TP5框架model怎么增刪改查、聚合、時(shí)間戳、軟刪除”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
文章標(biāo)題:TP5框架model怎么增刪改查、聚合、時(shí)間戳、軟刪除-創(chuàng)新互聯(lián)
文章出自:http://www.chinadenli.net/article36/dgecsg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、網(wǎng)站設(shè)計(jì)公司、小程序開發(fā)、動(dòng)態(tài)網(wǎng)站、App開發(fā)、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容