当前位置:网站首页>yii2 中andWhere多个or查询 orm条件
yii2 中andWhere多个or查询 orm条件
2022-07-03 02:42:00 【华大哥】
yii2中,经常会使用很多条件进行组合判断查询数据,和laravel 中不同,laravel 闭包函数查询
$data = Student::where(function ($query) use ($request) {
$id = $request->input(‘id‘);
$name = $request->input(‘name‘);
$query->where(‘id‘, ‘like‘, ‘%‘ . $id . ‘%‘)->where(‘sname‘, ‘like‘, ‘%‘ . $name . ‘%‘);
})->get()->toArray();
那么在yii2中多条件没有闭包函数,我们需要把条件通过判断封装到where条件里面,
字符串和哈希格式很好理解,就是直接把值和变量绑定 起来,这种更加简单。我们来看看操作符格式,因为操作符格式可以组成相对复杂的查询语句。下面我们查询一个商店订单的给的例子
$where = ['and'];
if(!empty($data['order_no'])) {
array_push($where,['like','order_no','%'.trim($data['order_no']).'%', false]);
}
if(!empty($data['shop_ids']) && is_array($data['shop_ids'])) {
$where[] = ['in','shop_id',$data['shop_ids']];
}
$query = Shop::find()
->where($where)
->select(['shop.name','order.money'])
->leftJoin('order','order.id=shop.order_id');
if($data['group'] !=2 ){
$query->orderBy('(CASE
WHEN status = 1 THEN 2
WHEN status = 2 THEN 1
WHEN status = 3 THEN 0
WHEN status = 4 THEN 0
WHEN status = 5 THEN 0
WHEN status = 6 THEN 0
END) desc')
->addOrderBy('shop.pay_time', 'desc');
}
$query = $query->addOrderBy('shop.create_time', 'desc');
$count = $query->count();
$pagination = new Pagination(['count' => $count, 'pageSize' => $pageSize, 'page' => $page - 1]);
$offset = ($page - 1) * $pagination->limit;
$list = $query->offset($offset)->limit($pagination->limit)->all();
这样一来,就把闭包函数处理成where条件的形式。
边栏推荐
猜你喜欢
Summary of interview project technology stack
Didi programmers are despised by relatives: an annual salary of 800000 is not as good as two teachers
【翻译】后台项目加入了CNCF孵化器
基于can总线的A2L文件解析(2)
easyExcel
Deep learning: multi-layer perceptron and XOR problem (pytoch Implementation)
错误Invalid bound statement (not found): com.ruoyi.stock.mapper.StockDetailMapper.xxxx解决
Deep Reinforcement Learning for Intelligent Transportation Systems: A Survey 论文阅读笔记
[flutter] example of asynchronous programming code between future and futurebuilder (futurebuilder constructor setting | handling flutter Chinese garbled | complete code example)
A2L file parsing based on CAN bus (2)
随机推荐
Gbase 8C system table PG_ cast
C语言中左值和右值的区别
GBase 8c系统表-pg_aggregate
[tutorial] chrome turns off cross domain policies CORS and samesite, and brings cookies across domains
The solution of "the required function is not supported" in win10 remote desktop connection is to modify the Registry [easy to understand]
GBase 8c触发器(二)
GBase 8c系统表-pg_amop
Apple releases MacOS 11.6.4 update: mainly security fixes
Didi programmers are despised by relatives: an annual salary of 800000 is not as good as two teachers
Tongda OA V12 process center
Gbase 8C create user / role example 2
"Analysis of 43 cases of MATLAB neural network": Chapter 43 efficient programming skills of neural network -- Discussion Based on the characteristics of the new version of MATLAB r2012b
GBase 8c 创建用户/角色 示例二
javeScript 0.1 + 0.2 == 0.3的问题
GBase 8c 触发器(一)
处理数据集,使用LabelEncoder将所有id转换为从0开始
Source code analysis | resource loading resources
Word word word
【ROS进阶篇】第六讲 ROS中的录制与回放(rosbag)
Build a private cloud disk cloudrev