开发文档 v2.x

更新/删除/清空表/删表

更新时间:2023年9月11日 22:35 浏览:200

 

update - 更新数据

public function update(array $keyValues = []): Table

参数:

  • array $keyValues - 要更新的数据键值对

返回值:

  • Table - 表模型本身

 

代码示例:

Be::getTable('user')
  ->where('age', 18)
  ->update([
    'age' => 19, 
    'update_time' => date('Y-m-d H:i:s'),
  ]);

 

increase - 自增某个字段

public function increase(string $field, int $step = 1): Table

参数:

  • string $field - 字段名
  • int $step - 自增量

返回值:

  • Table - 表模型本身

 

代码示例:

Be::getTable('user')
  ->where('age', '<', 18);
  ->increment('age', 1);
// 将所有小于 18 岁的用户年龄自增1

 

decrease - 自减某个字段

public function decrease(string $field, int $step = 1): Table

参数:

  • string $field - 字段名
  • int $step - 自减量

返回值:

  • Table - 表模型本身

 

代码示例:

Be::getTable('user')
  ->where('age', '>', 18);
  ->decrement('age', 1);
// 将所有大于 18 岁的用户年龄自减1

 

delete - 删除数据

public function delete(string $tableName = null): Table

参数:

  • array $keyValues - 表名,有联表时需指定

返回值:

  • Table - 表模型本身

 

代码示例:

Be::getTable('user')
  ->where('age', 18)
  ->delete();

// 联表时删除某张表中的数据
// 删除无用户的角色
Be::getTable('user')
  ->rightJoin('user_role', 'user.role_id = user_role.id')
  ->where('user.role_id IS NULL')
  ->delete('user_role');

 

truncate - 清空表

public function truncate(): Table

参数:

返回值:

  • Table - 表模型本身

 

代码示例:

Be::getTable('user')->truncate();

 

drop - 删表

public function drop(): Table

参数:

返回值:

  • Table - 表模型本身

 

代码示例:

Be::getTable('user')->drop();

 

导航