本文實例講述了TP5框架model常見操作。分享給大家供大家參考,具體如下:
- 使用model 查詢數(shù)據(jù),添加數(shù)據(jù),修改數(shù)據(jù),刪除數(shù)據(jù)
- 聚合操作
- 獲取器,修改器
- 自動添加時間戳(創(chuàng)建時間,修改時間)
- 軟刪除
1、使用model查詢數(shù)據(jù)
$res = User::get(1); //獲取主鍵為1的數(shù)據(jù),得到的是一個對象
$res = $res->toArray(); //將對象轉(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ù)查詢 id5 的記錄
$res = User::get(function($query){
$query->where("id","",5)
->field('name')
});
2、使用model添加數(shù)據(jù)
$res = User::create([
'name' => 'yulong',
'pwd' => '123'
],true); //第二個參數(shù)為true時,只添加數(shù)據(jù)表中已有的字段,不報錯,不寫則默認(rèn)為false;;;true 也可以換成一個數(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ù)更新 id5 的記錄
});
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() 里面有三個參數(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'); //id5 的記錄中的 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ù)據(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、自動往數(shù)據(jù)庫中添加時間戳
//自動往 time 字段中加入時間戳
public function setTimeAttr(){
return time();
}
//在數(shù)據(jù)添加時發(fā)生改變
protected $insert = [ 'time_insert' ]; //設(shè)置字段
public function setTimeInsertAttr(){ //將字段值設(shè)置為當(dāng)前時間
return time();
}
//在更新數(shù)據(jù)時發(fā)生改變
protected $update = [ 'time_update' ]; //設(shè)置字段
public function setTimeUpdateAttr(){ //將字段值設(shè)置為當(dāng)前時間
return time();
}
9、model時間戳
// 數(shù)據(jù)庫中的字段 create_time update_time
// database.php 中更改配置 'auto_timeStamp' => true
// 不推薦使用此方法,因為如果你的數(shù)據(jù)庫表中沒有 對應(yīng)的字段 ,程序可能就會報錯
// 可以單獨在 某個模型中 添加屬性
protected $autoWriteTimeStamp = true; //開啟自動加入時間戳
protected $createTime = 'create_at'; //設(shè)置 創(chuàng)建的時候?qū)懭?的字段 ,值可以為false,關(guān)閉操作
protedted $updateTime = 'update_at'; //設(shè)置 創(chuàng)建和更新的時候?qū)懭?的字段 ,值可以為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的記錄,第二個參數(shù)為 true 時,不是軟刪除,是tm真刪了
$ress = User::get(4);
$res = $ress->delete(true); // delete() 沒值時,為軟刪除;值為true,tm的真刪
// controller 獲取到 軟刪除 的記錄
$res = User::withTrashed(true)->find(1); //得到id為1 的經(jīng)過軟刪除 刪除的記錄
dump($res->getData()); //獲取原始數(shù)據(jù)
$res = User::onlyTrashed()->select(); //獲取所有軟刪除的數(shù)據(jù)
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對大家基于ThinkPHP框架的PHP程序設(shè)計有所幫助。
您可能感興趣的文章:- Thinkphp5.0框架使用模型Model的獲取器、修改器、軟刪除數(shù)據(jù)操作示例
- Thinkphp5.0 框架使用模型Model添加、更新、刪除數(shù)據(jù)操作詳解
- Thinkphp5.0 框架Model模型簡單用法分析
- thinkphp5 模型實例化獲得數(shù)據(jù)對象的教程
- ThinkPHP5&5.1框架關(guān)聯(lián)模型分頁操作示例
- Thinkphp5.0框架的Db操作實例分析【連接、增刪改查、鏈?zhǔn)讲僮鞯取?/li>
- ThinkPHP5.1框架數(shù)據(jù)庫鏈接和增刪改查操作示例
- tp5(thinkPHP5)框架數(shù)據(jù)庫Db增刪改查常見操作總結(jié)