Summary of database operation return values
0. Preface
1. Db operation database
1.1 New
$data = ['username' => 'bar', 'password' => 'foo'];
$res = Db::name('user')->insert($data);
return value:
- Success: return
1
- Failure: throw
异常
1.2 Update
// 方式1:save方法包含主键
$res = Db::name('user')->save(['id' => 7, 'username' => 'thinkphp1']);
// 方式2:update方法where条件更新
$res = Db::name('user')->where('id', 7)->update(['username' => 'thinkphp']);
// 方式3:update方法包含主键
$res = Db::name('user')->update(['username' => 'thinkphp','id' => 1]);
return value:
- Success: return
1
- Failure: return if no update occurs
0
; throw if the update field data table does not exist异常
1.3 Delete
1.3.1 Single delete
// 根据主键删除
$res = Db::name('user')->delete(7);
// 条件删除
$res = Db::name('user')->where('id',7)->delete();
return value:
- Success: return
1
- Failure: return
0
(including id does not exist)
1.3.2 Batch delete
// 根据主键删除
$res = Db::name('user')->delete([11,12]);
// 条件删除
$res = Db::name('user')->where('id','>',10)->delete();
return value:
- Success: Return the number of deleted records
- Failure: return
0
(including id does not exist)
1.4 Query
1.4.1 Single record
$res = Db::name('user')->where('id', 16)->find();
return value:
- Success: return
结果数组
- Failure: return
null
1.4.2 Multiple records
$res = Db::name('user')->where('status', 1)->select();
return value:
- Success: return
数据集对象
- Failure: return
空数据集对象
If you need to convert to an array, you can use the toArray()
method:
Db::name('user')->where('status', 1)->select()->toArray();
The return result is an array:
2. Model operation database
2.1 New
$user = new UserModel;
$user->username = "aaa";
$user->password = "aaa";
$res = $user->save();
dump($res);
// 也可直接把数据写在save方法里面:
$user->save([
'username' => 'thinkphp',
'password' => '123456'
]);
return value:
save
The method will returntrue
if it succeeds , and only when thebefore_insert
eventfalse
returnsfalse
, once there is an error抛出异常
. So there is no need to determine the return type.
2.2 Update
2.2.1 Single update
$user = UserModel::find(16);
$user->username = 'qwer';
$res = $user->save();
dump($res);
save
The method returns successfullytrue
, and returns only when thebefore_update
eventfalse
returnsfalse
, if there is an error抛出异常
.
2.2.2 Batch update
$user = new UserModel;
$list = [
['id'=>16, 'username'=>'thinkphp'],
['id'=>17, 'username'=>'onethink']
];
$res = $user->saveAll($list);
dump($res);
What is returned is a
数据集对象
2.3 Delete
2.3.1 Delete model
$user = UserModel::find(16);
$res = $user->delete();
dump($res);
The delete returns successfully
true
, an exception may be thrown if it fails
2.3.2 Delete based on the primary key
User::destroy(1);
// 支持批量删除多个数据
User::destroy([1,2,3]);
Return if the primary key does not exist
true
2.3.3 Conditional deletion
User::where('id','>',10)->delete();
Return deleted
记录数
2.4 Query
2.4.1 Single record
$user = UserModel::find(18);
// where查询
$user = UserModel::where('username', 'aaaa789')->find();
Return
模型数据
, the data is in the data parameter; if the query fails, returnnull
2.4.2 Multiple records
$users = UserModel::select([18,19]);
// where查询
$users = UserModel::where('status', 1)->select();
Return
模型集合
; return if the data does not exist空模型集合