ThinkPHP6 project basic operation (20. Summary of the return value of the actual part of the database operation)

0. Preface

1. Db operation database

1.1 New

$data = ['username' => 'bar', 'password' => 'foo'];
$res = Db::name('user')->insert($data);

return value:

  • Success: return1
  • 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: return1
  • 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: return1
  • 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: returnnull

1.4.2 Multiple records

$res = Db::name('user')->where('status', 1)->select();

return value:

  • Success: return数据集对象
    Insert picture description here
  • Failure: return空数据集对象
    Insert picture description here

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:
Insert picture description here

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:

saveThe method will return trueif it succeeds , and only when the before_insertevent falsereturns false, 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);

saveThe method returns successfully true, and returns only when the before_updateevent falsereturns false, 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 existtrue

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空模型集合

Guess you like

Origin blog.csdn.net/zy1281539626/article/details/112133685