|
在model/shop.php 中,通常我会采取关联 来获得商品分类:[ 撸代码 ]- <?php
- namespace app\index\model;
- use think\Model;
- /**
- name 商品的表
- */
- class Shop extends Model
- {
-
-
- // 关联商品类型
- public function proType()
- {
-
- return $this->hasOne('shoptype','id','type')->field('id,title');
- }
- }
复制代码
然而,时不时删掉分类 会导致我 前端页面渲染后报错, 所以 我在 list 获取接口 是这么写的:
- public function allShops()
- {
-
- // 页码
- $page = input('page');
- // 每页条数
- $limit = input('limit');
- // limit 初始条数
- $binLimit = ($page-1)*$limit;
- // 判断是否删除了
- $map['status'] = ['<>',0];
- // 关键字搜索
- if(!empty(input('key')) ){
- $map['title'] = ['like','%'.input('key').'%'];
- }
-
- // 总数
- $count = Db::name('shop')
- ->where($map)
- ->count();
-
- $req = Shop::order('id desc,status asc')
- ->where($map)
- ->limit($binLimit,$limit)
- ->field('id , title, initialPrice, consultPrice, marketPrice, start, end, province, city, area, picUrl, scene, type,auction_status,fabudate')
- ->select();
-
-
- foreach( $req as $k => $v )
- {
- //拿到分类
- if($v->proType)
- {
- $v['pType'] = $v->proType->title;
- }else{
- $v['pType'] = '无';
- }
- }
- downjson(0,"获取成功",$count, $req);
- }
复制代码
|
|