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数据,将前台传来 ...
随机推荐
- wamp 下运行Drupal慢的解决方法
1.Editing your php.ini and make realpath_cache_size=2M, 2.uncomment skip innodb in your my.cnf(my.in ...
- mysql存储方式MyISAM 和 InnoDB的区别
MyISAM 和 InnoDB 讲解: InnoDB和MyISAM是许多人在使用MySQL时最常用的两个表类型,这两个表类型各有优劣,视具体应用而定.基本的差别为:MyISAM类型不支持事务处理等高级 ...
- SqlServer作业指定目标服务器
用SSMS生成数据库作业的创建脚本的时候,有一步是sp_add_jobserver操作: EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = ...
- Graylog+elasticsearch+mongodb集群+nginx负载均衡前端
网上有张图画的很好,搜索有关它的配置文章,google里有几篇英文的,都是依靠haproxy等或别的什么实现,没有纯粹的Graylog+elasticsearch+mongodb集群,项目需要,只有自 ...
- C++ list类详解
转自:http://blog.csdn.net/whz_zb/article/details/6831817 双向循环链表list list是双向循环链表,,每一个元素都知道前面一个元素和后面一个元素 ...
- windows环境下Nginx部署及Https设置
一.Nginx安装部署及常用命令. 1.1.其实Nginx是免安装的.直接在官网下载zip包,解压即可,下载地址:http://nginx.org/en/download.html,因为我这边的开发服 ...
- X86/X64 函数调用约定
C 语言有 __cdecl.__stdcall.__fastcall.naked.__pascal. C++ 语言有 __cdecl.__stdcall.__fastcall.naked.__pasc ...
- pc端的动态效果
一 图片围绕着某一个确定的圆心运动 几张图片在一个圆开始运动的几种情况 https://github.com/GainLoss/Plug-in-unit 这里面有两种情况
- EOS签名R值过大导致报错"is_canonical( c ): signature is not canonical"
简要 EOS中规定签名的R和S必须同时小于N/2才是合法的签名. 详细 EOS签名交易相对BTC和ETH来说,对签名的要求更加严格了. BTC中bip62规定了((Low S values in si ...
- Aizu 0121 Seven Puzzle(变进制数的完美hash)
一遍预处理跑完所有情况,O(1)回答就好.状态记录我用的康拓和逆康拓. #include<bits/stdc++.h> using namespace std; ]; ]; ]; int ...