当前位置:网站首页>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 .
边栏推荐
- 左值右指解释的比较好的
- Monitoring and management of JVM
- 【Flutter】shared_ Preferences local storage (introduction | install the shared_preferences plug-in | use the shared_preferences process)
- Javescript 0.1 + 0.2 = = 0.3 problem
- tensor中的append应该如何实现
- GBase 8c系统表-pg_attribute
- 【富瀚6630编码存录像,用rtsp服务器及时间戳同步实现vlc观看录像】
- Gbase 8C system table PG_ database
- A2L file parsing based on CAN bus (2)
- Your family must be very poor if you fight like this!
猜你喜欢

Deep reinforcement learning for intelligent transportation systems: a survey paper reading notes

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

Error invalid bound statement (not found): com ruoyi. stock. mapper. StockDetailMapper. XXXX solution
![[shutter] banner carousel component (shutter_wiper plug-in | swiper component)](/img/a6/5c97ef3f34702b83ebf0511501d757.gif)
[shutter] banner carousel component (shutter_wiper plug-in | swiper component)

Summary of interview project technology stack

错误Invalid bound statement (not found): com.ruoyi.stock.mapper.StockDetailMapper.xxxx解决

Kubernetes cluster log and efk architecture log scheme

Add automatic model generation function to hade
![[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)](/img/6a/ae44ddb090ce6373f04a550a15f973.jpg)
[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)

超好用的日志库 logzero
随机推荐
Pytest (6) -fixture (Firmware)
sql server 查詢指定錶的錶結構
HW initial preparation
SqlServer行转列PIVOT
yii2 中andWhere多个or查询 orm条件
为什么会选择框架?选择什么样的框架
Counter统计数量后,如何返回有序的key
二维格式数组格式索引下标连续问题导致 返回json 格式问题
What is the way out for children from poor families?
GBase 8c系统表pg_cast
Error invalid bound statement (not found): com ruoyi. stock. mapper. StockDetailMapper. XXXX solution
《MATLAB 神经网络43个案例分析》:第43章 神经网络高效编程技巧——基于MATLAB R2012b新版本特性的探讨
Gbase 8C system table PG_ amop
Didi programmers are despised by relatives: an annual salary of 800000 is not as good as two teachers
Basic operation of binary tree (C language version)
random shuffle注意
面试八股文整理版
[shutter] banner carousel component (shutter_wiper plug-in | swiper component)
Getting started | jetpack hilt dependency injection framework
SQL statement