本文實(shí)例講述了Thinkphp5.0 框架使用模型Model添加、更新、刪除數(shù)據(jù)操作。分享給大家供大家參考,具體如下:
Thinkphp5.0 的使用模型Model添加數(shù)據(jù)
使用create()方法添加數(shù)據(jù)
$res = TestUser::create([
'name' => 'zhao liu',
'password' => md5(123456),
'email' => 'zhaoliu@qq.com'
]);
dump($res);
使用save()方法添加數(shù)據(jù)
$userModel = new TestUser;
$userModel->name = 'ya ya';
$userModel->email = 'yaya@139.com';
$res = $userModel->save();
dump($res);//影響的行數(shù)
dump($userModel->id);//新紀(jì)錄的id
注意:使用allowField(true)方法,傳遞不存在的字段時(shí)不會(huì)報(bào)錯(cuò)
示例:
$userModel = new TestUser;
$userModel->name = 'hei hei';
$userModel->email = 'heihei@139.com';
$userModel->yes = '不存在字段';
$res = $userModel->allowField(true)->save();
dump($res);//影響的行數(shù)
dump($userModel->id);//新紀(jì)錄的id
使用saveAll()方法添加多條數(shù)據(jù)
$userModel = new TestUser;
$data = array(
['name'=>'ga ga','email'=>'gaga@sina.com'],
['name'=>'you you','email'=>'youyou@163.com']
);
//返回結(jié)果是個(gè)多維的數(shù)組
$res = $userModel->saveAll($data);
//如果需要得到添加的數(shù)據(jù)的每個(gè)id,需要遍歷
foreach($res as $v){
dump($v->id);
}
Thinkphp5.0 的使用模型Model更新數(shù)據(jù)
(1)使用update()方法進(jìn)行更新數(shù)據(jù)
一、where條件寫在更新數(shù)據(jù)中
(這種情況更新的數(shù)據(jù),必須含主鍵)
$res = User::update([
'id' => 2,
'email' => '121@qq.com'
]);
//返回修改之后model的整個(gè)對(duì)象信息
dump($res);
二、where條件使用update()的第二個(gè)參數(shù),傳遞數(shù)組
$res = User::update([
'email' => '123@qq.com'
],['id'=>2]);
//返回修改之后model的整個(gè)對(duì)象信息
dump($res);
三、where條件使用update()的第二個(gè)參數(shù),傳遞閉包函數(shù)
$res = User::update([
'email' => '555@qq.com'
],function($query){
$query->where(['id'=>2]);
});
//返回修改之后model的整個(gè)對(duì)象信息
dump($res);
四、使用where條件
$res = User::where('id','=',2)->update([
'email'=>'666@qq.com'
]);
//返回影響的行數(shù)
dump($res);
(2)使用save()方法
方式一:
$model = User::get(2);
$model->email = '777@qq.com';
$res = $model->save();
//返回影響的行數(shù)
dump($res);
方式二:
$model = new User();
$res2 = $model->save([
'email' => '999@qq.com'
],['id'=>2]);
//返回影響的行數(shù)
dump($res2);
方式三:
$model = new User();
$res = $model->save([
'email' => '000@qq.com'
],function($query){
$query->where(['id'=>2]);
});
//返回影響的行數(shù)
dump($res);
使用saveAll()方法更新多個(gè)數(shù)據(jù):
$model = new User();
$res = $model->saveAll([
['id' => 2,'email' => '122@qq.com'],
['id' => 3,'email' => '123@qq.com'],
['id' => 4,'email' => '124@qq.com']
]);
//返回?cái)?shù)組
dump($res);
Thinkphp5.0 的使用模型Model刪除數(shù)據(jù)
一、使用destory()刪除數(shù)據(jù)
//刪除id為3的記錄
$res = User::destroy(3);
//返回影響的行數(shù)
dump($res);
destory()的參數(shù)可以是主鍵、數(shù)組條件、閉包函數(shù)。
二、使用delete()刪除數(shù)據(jù)
//刪除id為3的記錄
$model = User::get(3);
$res = $model->delete();
//返回影響的行數(shù)
dump($res);
三、delete()和where()
//刪除id為4的記錄
$res = User::where('id','=',4)->delete();
//返回影響的行數(shù)
dump($res);
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對(duì)大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:- Thinkphp5.0 框架Model模型簡(jiǎn)單用法分析
- Thinkphp5.0框架使用模型Model的獲取器、修改器、軟刪除數(shù)據(jù)操作示例
- thinkphp5.1的model模型自動(dòng)更新update_time字段實(shí)例講解