当前位置:网站首页>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条件的形式。
边栏推荐
- easyPOI
- Producer consumer model based on thread pool (including blocking queue)
- GBase 8c系统表-pg_am
- 2022-2028 global splicing display industry research and trend analysis report
- "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 system table PG_ aggregate
- Random shuffle note
- The difference between left value and right value in C language
- 为什么会选择框架?选择什么样的框架
- Basic operation of binary tree (C language version)
猜你喜欢

怎么将yolov5中的PANet层改为BiFPN
![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

What does "where 1=1" mean

MATLAB小技巧(24)RBF,GRNN,PNN-神经网络

Check log4j problems using stain analysis

Kubernetes family container housekeeper pod online Q & A?

Didi programmers are despised by relatives: an annual salary of 800000 is not as good as two teachers

What is the way out for children from poor families?

Random Shuffle attention

4. Classes and objects
随机推荐
Deep Reinforcement Learning for Intelligent Transportation Systems: A Survey 论文阅读笔记
HTB-Devel
Gbase 8C system table PG_ amop
GBase 8c系统表pg_database
4. Classes and objects
Random shuffle note
【 tutoriel】 Chrome ferme les cors et les messages de la politique inter - domaines et apporte des cookies à travers les domaines
Summary of interview project technology stack
Kubernetes family container housekeeper pod online Q & A?
Didi programmers are despised by relatives: an annual salary of 800000 is not as good as two teachers
当lambda没有输入时,是何含义?
Gbase 8C system table PG_ database
[shutter] setup of shutter development environment (supplement the latest information | the latest installation tutorial on August 25, 2021)
Informatics Olympiad one general question bank 1006 a+b questions
[principles of multithreading and high concurrency: 1_cpu multi-level cache model]
[translation] modern application load balancing with centralized control plane
Javescript 0.1 + 0.2 = = 0.3 problem
GBase 8c系统表-pg_amproc
The solution of "the required function is not supported" in win10 remote desktop connection is to modify the Registry [easy to understand]
MUX VLAN Foundation