zoukankan      html  css  js  c++  java
  • tp5 使用paginate分页获取数据对象之后 如何对对象进行数据添加

    tp5 使用paginate分页获取数据对象之后 如何对对象进行数据添加

    大家都知道,在使用tp5的paginate获取分页数据之后,得到的是一个数据对象,但有时会碰到要对数据对象进行二次加工的情况,下面是解决此类问题的方法

    1、直接在查询语句中利用MySQL函数

    举例一:

    1、将获取到的图片由相对地址拼接上域名,形成绝对地址

    1  $yu = YU();
    2  return $this->field('orderid,productid,attrid,concat("'.$yu.'", logo) logo,title')
    3      ->where(['orderid'=>$OrderId])->paginate(10);

    2、将时间戳转换成所需要的日期格式

    1 return $this->where($where)->with('user')
    2     ->field("*,FROM_UNIXTIME(createtime,'%Y-%m-%d') createtime")
    3     ->order('createtime desc')
    4     ->select();

    关于时间戳与日期转换,请参考MySQL时间戳与日期互相转换

    举例二:每个商品有不同的规格数据,统计每个商品所有规格的总库存

     1  $products = Db::name('product p')
     2     ->field('itemid,name,m_price,price,logo,sale_num,sort,is_sale,is_floor,addtime,update_time')
     3     ->field('(select sum(stock) from xf_product_attr a where a.product_id = p.itemid) stock_all')
     4     ->whereOr($keywordComplex)
     5     ->where($where)
     6     ->order('itemid asc list_order asc')
     7     ->paginate(10,false,[
     8        'query'=>[
     9            'keyword'  =>$keyword,
    10            'is_sale'  =>$is_sale,
    11            'is_floor' =>$is_floor,
    12            'category' =>$category,
    13        ]
    14      ]);

    举例三:获得某种商品券的兑换总数,使用左连接

    1  return $this->with('shop')
    2     ->alias('p')
    3     ->join('order o','p.itemid = o.pro_id and o.status=2','left')
    4     ->whereOr($whereOr)
    5     ->where($where)
    6     ->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(*)')
    7     ->order('p.addtime desc')
    8     ->paginate(10,false,$query);

    可以看到生成的sql的语句是

    1 [ 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` 
    2         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 ]

    可使用子查询,不过效率就下来了

    1 return $this->with('shop')
    2    ->alias('p')
    3    ->whereOr($whereOr)
    4    ->where($where)
    5    ->field('*,(select count(*) from xf_order o where o.pro_id = p.itemid and o.status = 2)')
    6    ->order('p.addtime desc')
    7    ->paginate(10,false,$query);

    生成的sql是这样的:

    1 [ SQL ] SELECT *,(select count(*) from xf_order o where o.pro_id = p.itemid and o.status = 2) 
    2         FROM `xf_product` `p` ORDER BY `p`.`addtime` DESC LIMIT 0,10 [ RunTime:0.014000s ]

    2、还有一些复杂的,通过MySQL自带函数是实现不了的,此时可以将对象转换为数组,然后再处理

     1 } else {
     2     $products = Db::name('product p')
     3         ->field('itemid,name,m_price,price,logo,sale_num,sort,is_sale,is_floor,addtime,update_time')
     4         ->whereOr($keywordComplex)
     5         ->where($where)
     6         ->order('itemid asc list_order asc')
     7         ->paginate(10,false,[
     8             'query'=>[
     9                 'keyword'  =>$keyword,
    10                 'is_sale'  =>$is_sale,
    11                 'is_floor' =>$is_floor,
    12                 'category' =>$category,
    13             ]
    14         ]);
    15 }
    16 
    17 //查找商品自身的类别
    18 $products->toArray();
    19 foreach($products as $k=>$v){
    20     $data = $v;
    21     $data['category_id'] = Db::name('product_category_bind pb')
    22         ->view('category pc','id,name','pb.category_id = pc.id','left')
    23         ->where(['pb.product_id'=>$v['itemid']])
    24         ->select()->toArray();
    25     $products->offsetSet($k,$data);
    26 }
  • 相关阅读:
    做好技术的量的累积,实现业绩的质的飞跃|专访宜信财富技术负责人刘宝剑
    深入理解MySQL索引
    程序的一生:从源程序到进程的辛苦历程
    Serializable详解(1):代码验证Java序列化与反序列化
    关于Java序列化的问题你真的会吗?
    Dubbo源码解析之SPI(一):扩展类的加载过程
    [C#] 命令总线模式
    C#构造函数在继承时必须要求与父类型构造函数入参相同怎么办?
    如何通过JavaScript构建Asp.net服务端控件
    Javascript iframe交互并兼容各种浏览器的解决方案
  • 原文地址:https://www.cnblogs.com/cyfblogs/p/10083260.html
Copyright © 2011-2022 走看看