当前位置:网站首页>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 .
边栏推荐
- Error when installing MySQL in Linux: starting mysql The server quit without updating PID file ([FAILED]al/mysql/data/l.pid
- where 1=1 是什么意思
- [fluent] listview list (map method description of list set | vertical list | horizontal list | code example)
- Packing and unpacking of JS
- Gbase 8C system table PG_ amproc
- 《MATLAB 神经网络43个案例分析》:第43章 神经网络高效编程技巧——基于MATLAB R2012b新版本特性的探讨
- sql server 查询指定表的表结构
- Joking about Domain Driven Design (III) -- Dilemma
- Use cve-2021-43893 to delete files on the domain controller
- Xiaodi notes
猜你喜欢
Pytest (6) -fixture (Firmware)
Kubernetes family container housekeeper pod online Q & A?
Basic operation of binary tree (C language version)
Linear rectification function relu and its variants in deep learning activation function
Didi programmers are despised by relatives: an annual salary of 800000 is not as good as two teachers
Build a private cloud disk cloudrev
Joking about Domain Driven Design (III) -- Dilemma
[translation] modern application load balancing with centralized control plane
迅雷chrome扩展插件造成服务器返回的数据js解析页面数据异常
Add MDF database file to SQL Server database, and the error is reported
随机推荐
sql server 查询指定表的表结构
Xiaodi notes
Kubernetes cluster log and efk architecture log scheme
Packing and unpacking of JS
JMeter performance test JDBC request (query database to obtain database data) use "suggestions collection"
HW initial preparation
Global and Chinese ammonium dimolybdate market in-depth analysis and prospect risk prediction report 2022 Edition
面试八股文整理版
Pytest (6) -fixture (Firmware)
[translation] modern application load balancing with centralized control plane
[fluent] future asynchronous programming (introduction | then method | exception capture | async, await keywords | whencomplete method | timeout method)
简单理解svg
GBase 8c系统表-pg_aggregate
Tensorflow to pytorch notes; tf. gather_ Nd (x, y) to pytorch
[tutorial] chrome turns off cross domain policies CORS and samesite, and brings cookies across domains
【翻译】具有集中控制平面的现代应用负载平衡
Choose it when you decide
[advanced ROS] Lesson 6 recording and playback in ROS (rosbag)
[C语言]给账号密码进行MD5加密
Matlab tips (24) RBF, GRNN, PNN neural network