当前位置:网站首页>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条件的形式。
边栏推荐
- [fluent] futurebuilder asynchronous programming (futurebuilder construction method | asyncsnapshot asynchronous calculation)
- GBase 8c系统表-pg_conversion
- Compréhension simple de SVG
- Oauth2.0 authentication, login and access "/oauth/token", how to get the value of request header authorization (basictoken)???
- Gbase 8C system table PG_ class
- Gbase 8C trigger (III)
- Word word word
- Principle and application of database
- Gbase 8C system table PG_ constraint
- Counter统计数量后,如何返回有序的key
猜你喜欢

Practice of traffic recording and playback in vivo

Oauth2.0 authentication, login and access "/oauth/token", how to get the value of request header authorization (basictoken)???

How to change the panet layer in yolov5 to bifpn
【ROS进阶篇】第六讲 ROS中的录制与回放(rosbag)
![Error when installing MySQL in Linux: starting mysql The server quit without updating PID file ([FAILED]al/mysql/data/l.pid](/img/32/25771baad1ed06c5a592087df748f1.jpg)
Error when installing MySQL in Linux: starting mysql The server quit without updating PID file ([FAILED]al/mysql/data/l.pid

【翻译】后台项目加入了CNCF孵化器

Kubernetes family container housekeeper pod online Q & A?

random shuffle注意

定了,就选它
![[fluent] listview list (map method description of list set | vertical list | horizontal list | code example)](/img/e5/c01f760b07b495f5b048ea367e0c21.gif)
[fluent] listview list (map method description of list set | vertical list | horizontal list | code example)
随机推荐
HW-初始准备
为什么会选择框架?选择什么样的框架
Gbase 8C system table PG_ aggregate
MATLAB小技巧(24)RBF,GRNN,PNN-神经网络
Can netstat still play like this?
Monitoring and management of JVM
HTB-Devel
GBase 8c系统表-pg_auth_members
GBase 8c触发器(二)
Add automatic model generation function to hade
The core idea of performance optimization, dry goods sharing
左值右指解释的比较好的
Deep Reinforcement Learning for Intelligent Transportation Systems: A Survey 论文阅读笔记
Classes and objects - initialization and cleanup of objects - constructor call rules
random shuffle注意
What is the way out for children from poor families?
SQL statement
[principles of multithreading and high concurrency: 1_cpu multi-level cache model]
Error invalid bound statement (not found): com ruoyi. stock. mapper. StockDetailMapper. XXXX solution
JMeter performance test JDBC request (query database to obtain database data) use "suggestions collection"