当前位置:网站首页>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 .
边栏推荐
- [shutter] banner carousel component (shutter_wiper plug-in | swiper component)
- What does "where 1=1" mean
- MATLAB小技巧(24)RBF,GRNN,PNN-神经网络
- Unity3d human skin real time rendering real simulated human skin real time rendering "suggestions collection"
- Deep learning: multi-layer perceptron and XOR problem (pytoch Implementation)
- Simple understanding of SVG
- 【翻译】Flux安全。通过模糊处理获得更多信心
- Two dimensional format array format index subscript continuity problem leads to return JSON format problem
- What is the way out for children from poor families?
- MUX VLAN Foundation
猜你喜欢

Add MDF database file to SQL Server database, and the error is reported

Check log4j problems using stain analysis
![[Hcia]No.15 Vlan间通信](/img/59/a467c5920cbccb72040f39f719d701.jpg)
[Hcia]No.15 Vlan间通信

面试项目技术栈总结

where 1=1 是什么意思

HW-初始准备
![[shutter] bottom navigation bar page frame (bottomnavigationbar bottom navigation bar | pageview sliding page | bottom navigation and sliding page associated operation)](/img/6e/67bc187a89fb9125856c78c89f7bfb.gif)
[shutter] bottom navigation bar page frame (bottomnavigationbar bottom navigation bar | pageview sliding page | bottom navigation and sliding page associated operation)

Your family must be very poor if you fight like this!

What does "where 1=1" mean

MATLAB小技巧(24)RBF,GRNN,PNN-神经网络
随机推荐
Privatization lightweight continuous integration deployment scheme -- 01 environment configuration (Part 2)
Unity3d human skin real time rendering real simulated human skin real time rendering "suggestions collection"
Gbase 8C function / stored procedure parameters (II)
ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc
C语言初阶-指针详解-庖丁解牛篇
基于can总线的A2L文件解析(2)
Three.js本地环境搭建
Super easy to use logzero
Basic operation of binary tree (C language version)
Strategy application of Dameng database
Kubernetes family container housekeeper pod online Q & A?
C语言中左值和右值的区别
Joking about Domain Driven Design (III) -- Dilemma
【翻译】Flux安全。通过模糊处理获得更多信心
The Linux server needs to install the agent software EPS (agent) database
Gbase 8C function / stored procedure definition
Gbase 8C system table PG_ aggregate
[translation] modern application load balancing with centralized control plane
Process the dataset and use labelencoder to convert all IDs to start from 0
错误Invalid bound statement (not found): com.ruoyi.stock.mapper.StockDetailMapper.xxxx解决