魔众人才招聘系统PHP库-数据库-ModelUtil

魔众人才招聘系统 / 文档中心
文档中心
开发教程
安装常见问题

model 构建模型

ModelUtil::model( $model )

  • 参数:$model string 数据表
  • 返回:Model|Builder 数据库模型

代码示例

// 查询
ModelUtil::model('user')->where(['id'=>1])->get()->toArray();
ModelUtil::model('user')->where('id','>',5)->get()->toArray();
// 查询-like
ModelUtil::model('user')->where('username','like','%keywords%')->get()->toArray();
// 查询-limit
ModelUtil::model('user')->limit(5)->get()->toArray();
// 查询-原生SQL
ModelUtil::model('user')->whereRaw(DB::raw('id > 0 OR id is null'))->get()->toArray();

// 删除
ModelUtil::model('user')->where(['id'=>1])->delete();

// 更新
ModelUtil::model('user')->where(['id'=>1])->update(['username'=>'aaa']);

insert 插入数据

ModelUtil::insert( $model, $data )

  • 参数:$model string 数据表
  • 参数:$data array 数据数组
  • 返回:array 插入的数据记录

代码示例

ModelUtil::insert('user',['username'=>'aaa','nickname'=>'bbb']);

insertAll 插入多条数据

ModelUtil::insertAll( $model, $datas, $updateTimestamp )

  • 参数:$model string 数据表
  • 参数:$datas array 多条数据数组
  • 参数:$updateTimestamp boolean 是否更新时间戳,默认为true
  • 返回:void

代码示例

ModelUtil::insertAll('user',[ ['username'=>'aaa','nickname'=>'bbb'], ['username'=>'ccc','nickname'=>'ddd'] ]);

delete 删除记录

ModelUtil::delete( $model, $where )

  • 参数:$model string 数据表
  • 参数:$where array|int 条件数组或数据ID
  • 返回:integer 被删除的记录数量

代码示例

// 删除ID为1的用户
ModelUtil::delete('user',1);
// 删除用户名为aaa的用户
ModelUtil::delete('user',['username'=>'aaa']);

get 获取单条记录

ModelUtil::get( $model, $where, $fields, $order )

  • 参数:$model string 数据表
  • 参数:$where int|array 条件
  • 参数:$fields array 数据表字段
  • 参数:$order array 排序,如 ['id','asc']
  • 返回:array|null 数据记录

代码示例

ModelUtil::get('user',1);
ModelUtil::get('user',['username'=>'xxx']);
更复杂的数据获取可以使用 ModelUtil::model('xxx') 进行操作
QQ
微信
公众号