当前位置:网站首页>Andwhere multiple or query ORM conditions in yii2
Andwhere multiple or query ORM conditions in yii2
2022-07-03 02:46:00 【Brother Hua】
yii2 in , Many conditions are often used to judge and query data , and laravel Different from ,laravel Closure function query
$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();
So in yii2 There is no closure function in many conditions , We need to encapsulate the conditions into where In terms of ,
String and hash formats are well understood , Is to bind values and variables directly get up , This is simpler . Let's look at the operator format , Because the operator format can form relatively complex query statements . Let's check an example of a store order
$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();
thus , Treat the closure function as where The form of the condition .
边栏推荐
- GBase 8c系统表pg_cast
- Gbase 8C system table PG_ am
- Gbase 8C trigger (III)
- Privatization lightweight continuous integration deployment scheme -- 01 environment configuration (Part 2)
- The difference between left value and right value in C language
- A2L file parsing based on CAN bus (2)
- 疫情当头,作为Leader如何进行代码版本和需求开发管控?| 社区征文
- [shutter] banner carousel component (shutter_wiper plug-in | swiper component)
- 【翻译】具有集中控制平面的现代应用负载平衡
- SqlServer行转列PIVOT
猜你喜欢
Kubernetes cluster log and efk architecture log scheme
面试项目技术栈总结
ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc
Classes and objects - initialization and cleanup of objects - constructor call rules
Linear rectification function relu and its variants in deep learning activation function
Error invalid bound statement (not found): com ruoyi. stock. mapper. StockDetailMapper. XXXX solution
What is the way out for children from poor families?
Choose it when you decide
xiaodi-笔记
Check log4j problems using stain analysis
随机推荐
Oauth2.0 authentication, login and access "/oauth/token", how to get the value of request header authorization (basictoken)???
左值右指解释的比较好的
Sqlserver row to column pivot
【富瀚6630编码存录像,用rtsp服务器及时间戳同步实现vlc观看录像】
Cron表达式介绍
MUX VLAN Foundation
[principles of multithreading and high concurrency: 1_cpu multi-level cache model]
GBase 8c系统表pg_cast
Process the dataset and use labelencoder to convert all IDs to start from 0
Two dimensional format array format index subscript continuity problem leads to return JSON format problem
[fluent] future asynchronous programming (introduction | then method | exception capture | async, await keywords | whencomplete method | timeout method)
Interview stereotyped version
Super easy to use logzero
The core idea of performance optimization, dry goods sharing
cvpr2022去雨去雾
用docker 连接mysql的过程
JS的装箱和拆箱
Didi programmers are despised by relatives: an annual salary of 800000 is not as good as two teachers
Javescript 0.1 + 0.2 = = 0.3 problem
[fluent] JSON model conversion (JSON serialization tool | JSON manual serialization | writing dart model classes according to JSON | online automatic conversion of dart classes according to JSON)