当前位置:网站首页>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条件的形式。
边栏推荐
- 疫情当头,作为Leader如何进行代码版本和需求开发管控?| 社区征文
- [tutorial] chrome turns off cross domain policies CORS and samesite, and brings cookies across domains
- random shuffle注意
- [Hcia]No.15 Vlan间通信
- GBase 8c 函数/存储过程参数(二)
- The core idea of performance optimization, dry goods sharing
- Gbase 8C trigger (III)
- 面试项目技术栈总结
- Xiaodi notes
- 【ROS进阶篇】第六讲 ROS中的录制与回放(rosbag)
猜你喜欢
Tongda OA V12 process center
[Hcia]No.15 Vlan间通信
超好用的日志库 logzero
Xiaodi notes
HTB-Devel
Thread safe singleton mode
The data in servlet is transferred to JSP page, and the problem cannot be displayed using El expression ${}
HW initial preparation
Mathematical statistics -- Sampling and sampling distribution
[shutter] setup of shutter development environment (supplement the latest information | the latest installation tutorial on August 25, 2021)
随机推荐
[Hcia]No.15 Vlan间通信
Packing and unpacking of JS
QT qcombobox add qccheckbox (drop-down list box insert check box, including source code + comments)
[translation] modern application load balancing with centralized control plane
二维格式数组格式索引下标连续问题导致 返回json 格式问题
The core idea of performance optimization, dry goods sharing
Simple understanding of SVG
Source code analysis | layout file loading process
GBase 8c系统表-pg_conversion
Strategy application of Dameng database
JMeter performance test JDBC request (query database to obtain database data) use "suggestions collection"
[flutter] example of asynchronous programming code between future and futurebuilder (futurebuilder constructor setting | handling flutter Chinese garbled | complete code example)
Apple releases MacOS 11.6.4 update: mainly security fixes
Gbase 8C trigger (III)
GBase 8c系统表pg_cast
错误Invalid bound statement (not found): com.ruoyi.stock.mapper.StockDetailMapper.xxxx解决
Gbase 8C system table PG_ cast
leetcode540
GBase 8c 创建用户/角色 示例二
Getting started | jetpack hilt dependency injection framework