tp5 使用paginate分页获取数据对象之后 如何对对象进行数据添加
tp5 使用paginate分页获取数据对象之后 如何对对象进行数据添加
大家都知道,在使用tp5的paginate获取分页数据之后,得到的是一个数据对象,但有时会碰到要对数据对象进行二次加工的情况,下面是解决此类问题的方法
1、直接在查询语句中利用MySQL函数
举例一:
1、将获取到的图片由相对地址拼接上域名,形成绝对地址
$yu = YU();
return $this->field('orderid,productid,attrid,concat("'.$yu.'", logo) logo,title')
->where(['orderid'=>$OrderId])->paginate(10);
2、将时间戳转换成所需要的日期格式
return $this->where($where)->with('user')
->field("*,FROM_UNIXTIME(createtime,'%Y-%m-%d') createtime")
->order('createtime desc')
->select();
关于时间戳与日期转换,请参考MySQL时间戳与日期互相转换
举例二:每个商品有不同的规格数据,统计每个商品所有规格的总库存
$products = Db::name('product p')
->field('itemid,name,m_price,price,logo,sale_num,sort,is_sale,is_floor,addtime,update_time')
->field('(select sum(stock) from xf_product_attr a where a.product_id = p.itemid) stock_all')
->whereOr($keywordComplex)
->where($where)
->order('itemid asc list_order asc')
->paginate(10,false,[
'query'=>[
'keyword' =>$keyword,
'is_sale' =>$is_sale,
'is_floor' =>$is_floor,
'category' =>$category,
]
]);
举例三:获得某种商品券的兑换总数,使用左连接
return $this->with('shop')
->alias('p')
->join('order o','p.itemid = o.pro_id and o.status=2','left')
->whereOr($whereOr)
->where($where)
->field('p.itemid,p.name,p.title,p.avatar,p.point,p.status,p.addtime,p.list_order, p.shop_id,p.check_status,count(*)')
->order('p.addtime desc')
->paginate(10,false,$query);
可以看到生成的sql的语句是
[ SQL ] SELECT p.itemid,p.name,p.title,p.avatar,p.point,p.status,p.addtime,p.list_order, p.shop_id,p.check_status,count(*) FROM `xf_product` `p`
LEFT JOIN `xf_order` `o` ON `p`.`itemid`=o.pro_id and o.status=2 ORDER BY `p`.`addtime` DESC LIMIT 0,10 [ RunTime:0.000000s ]
也可使用子查询,不过效率就下来了
return $this->with('shop')
->alias('p')
->whereOr($whereOr)
->where($where)
->field('*,(select count(*) from xf_order o where o.pro_id = p.itemid and o.status = 2)')
->order('p.addtime desc')
->paginate(10,false,$query);
生成的sql是这样的:
[ SQL ] SELECT *,(select count(*) from xf_order o where o.pro_id = p.itemid and o.status = 2)
FROM `xf_product` `p` ORDER BY `p`.`addtime` DESC LIMIT 0,10 [ RunTime:0.014000s ]
2、还有一些复杂的,通过MySQL自带函数是实现不了的,此时可以将对象转换为数组,然后再处理
} else {
$products = Db::name('product p')
->field('itemid,name,m_price,price,logo,sale_num,sort,is_sale,is_floor,addtime,update_time')
->whereOr($keywordComplex)
->where($where)
->order('itemid asc list_order asc')
->paginate(10,false,[
'query'=>[
'keyword' =>$keyword,
'is_sale' =>$is_sale,
'is_floor' =>$is_floor,
'category' =>$category,
]
]);
}
//查找商品自身的类别
$products->toArray();
foreach($products as $k=>$v){
$data = $v;
$data['category_id'] = Db::name('product_category_bind pb')
->view('category pc','id,name','pb.category_id = pc.id','left')
->where(['pb.product_id'=>$v['itemid']])
->select()->toArray();
$products->offsetSet($k,$data);
}
tp5 使用paginate分页获取数据对象之后 如何对对象进行数据添加的更多相关文章
- 请求Url返回数据较大,使结果分页获取
首先创建了一个单元测试,如下项目视图: 分页结果映射类PageResult的编写: using System; using System.Collections.Generic; using Syst ...
- 腾讯云图片鉴黄集成到C# SQL Server 怎么在分页获取数据的同时获取到总记录数 sqlserver 操作数据表语句模板 .NET MVC后台发送post请求 百度api查询多个地址的经纬度的问题 try{}里有一个 return 语句,那么紧跟在这个 try 后的 finally {}里的 code 会 不会被执行,什么时候被执行,在 return 前还是后? js获取某个日期
腾讯云图片鉴黄集成到C# 官方文档:https://cloud.tencent.com/document/product/641/12422 请求官方API及签名的生成代码如下: public c ...
- SQL Server 怎么在分页获取数据的同时获取到总记录数
SQL Server 获取数据的总记录数,有两种方式: 1.先分页获取数据,然后再查询一遍数据库获取到总数量 2.使用count(1) over()获取总记录数量 SELECT * FROM ( SE ...
- redis分页获取数据
php代码: 采用哈希类型存储数据,有序集合存储分页数据,进行倒序与正序的排序. $getGoodsInfo = M('goods_test')->select(); for($i=0;$i&l ...
- 【Django+Element UI】使用一个接口文件,搞定分页获取数据,模糊查询后分页获取数据
1:序列化获取数据的接口设计 1:分页获取序列化数据 2:是个能传参数的接口 class Society(APIView): def post(self, request): keywords = s ...
- thinkphp 使用paginate分页搜索带参数
最近做项目发现使用paginate分页,搜索的时候点下一页搜索条件就变没了,所以在网上找了找一些方法,有的说是使用Page类,但是用习惯了paginate,再用Page不习惯,找到了一个方法,可以使用 ...
- Asp.net MVC 传递数据 从前台到后台,包括单个对象,多个对象,集合
今天为大家分享下 Asp.net MVC 将数据从前台传递到后台的几种方式. 环境:VS2013,MVC5.0框架 1.基本数据类型 我们常见有传递 int, string, bool, double ...
- 《项目经验》--通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来的Json数据写入数据库表中
先看一下我要实现的功能界面: 这个界面的功能在图中已有展现,课程分配(教师教授哪门课程)在之前的页面中已做好.这个页面主要实现的是授课,即给老师教授的课程分配学生.此页面实现功能的步骤已在页面 ...
- 通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来的Json数据写入数据库表中
摘自:http://blog.csdn.net/mazhaojuan/article/details/8592015 通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来 ...
随机推荐
- Ubuntu14.04下sogou输入法的输入框只显示英文不显示中文的问题
解决方法:首先强制更新,把依赖文件全部安装 sudo apt-get install -f 如果仍然不管用,删除sogou的配置文件,在~/.config目录下,一般情况下是SogouPY.Sogou ...
- D - 稳住GCD DP
http://acm.uestc.edu.cn/#/problem/show/923 给定一堆数字,求其所有数字的gcd. 现在要删除最多的数字,使得剩下的数字的gcd和原来的一样. 设dp[i][v ...
- (转)不看绝对后悔的Linux三剑客之sed实战精讲
不看绝对后悔的Linux三剑客之sed实战精讲 原文:http://blog.51cto.com/hujiangtao/1923718 二.Linux三剑客之sed命令精讲 1,前言 我们都知道,在L ...
- RabbitMQ使用教程(三)如何保证消息99.99%被发送成功?
1. 前情回顾 RabbitMQ使用教程(一)RabbitMQ环境安装配置及Hello World示例 RabbitMQ使用教程(二)RabbitMQ用户管理,角色管理及权限设置 在以上两篇博客发布后 ...
- linq 读取xml
xml 文件如下: <?xml version="1.0" encoding="utf-8" ?><nodes> <node> ...
- androidStudio修改包名 Android 如何修改包名(同一个手机可以跑2个eros 项目)。
修改applicationId(gradle.properties). 2.即时同步更新过去,否则不报错
- <Android 基础(二)> BroadcastReceiver
介绍 BroadcastReceiver:广播接收者,很形象,广播发送,类比生活中的广播,有能力听得到的都可以介绍到这个信息,然后在大脑中反映.对应到Android中就是SendBroadcast和o ...
- 【extjs6学习笔记】0.3 准备: 类库结构2
- use scanner/smb/smb_version
use scanner/smb/smb_version msf auxiliary(smb_version) > set RHOSTS 172.16.21.170RHOSTS => 172 ...
- [:space:]的用法(转)
转自:http://blog.itpub.net/27181165/viewspace-1061688/ 在linux中通常会使用shell结合正则表达式来过滤字符,本文将以一个简单的例子来说明+,* ...