比如我有個數據庫是地址表名為
addr_school (存放的地址)
字段 id addr addname time
14 上元路滬城環路(紅綠燈東北角滬城環路共享區公交車站“橫穿滬城環路左轉直走即可”) 海洋.海事1 1605943133
15 上元路滬城環路(紅綠燈東南角滬城環路共享區公交車站“上元路滬城環路右轉直走即可”) 海洋.海事2 1605943153
17 電機2號門左轉50米 電機.建橋1【電機2號門左轉50米】 1605944583
18 建橋西門左轉100米公交站台 電機.建橋2【建橋西門左轉100米公交站台】 1605944747
19 電力1號門旁1077公交站 電力【電力1號門旁1077公交站】 1605944930
另一個表是訂單表 有上車點和下車點 表名為
route_school
上車點s_id 下車點e_id
此時上車點關聯了
public function addrschool() { return $this->belongsTo('AddrSchool', 's_id', 'id', [], 'LEFT')->setEagerlyType(0); }
但是下車點也需要關聯就需要加入以下代碼
在模型里面加入
public function addrschool2()//加入的 { return $this->belongsTo('AddrSchool', 'e_id', 'id', [], 'LEFT')->setEagerlyType(0); }
在控制器里面
list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $total = $this->model ->with(['addrschool','addrschool2','route','user','couponsuser','stime',])//加入了addrschool2 ->where($where) ->order($sort, $order) ->count(); $list = $this->model ->with(['addrschool','addrschool2','route','user','couponsuser','stime'])//加入了addrschool2 ->where($where) ->order($sort, $order) ->limit($offset, $limit) ->select(); foreach ($list as $row) { $row->visible(['id','uid','createtime','tel','status','order','updatetime','e_id','s_id','routeid','paytime','pay_status','tk_order','price','money','cou_id','stime_id','stime_date','tk_money','tk_time','scan_time','scan_status','num']); $row->visible(['addrschool']); $row->getRelation('addrschool')->visible(['addrname']); $row->visible(['addrschool2']);//加入的 $row->getRelation('addrschool')->visible(['addrname']);//加入的 QQ496631085 $row->visible(['route']); $row->getRelation('route')->visible(['name','type_status']); $row->visible(['user']); $row->getRelation('user')->visible(['nickname','wx_openid','headurl','vip']); $row->visible(['couponsuser']); $row->getRelation('couponsuser')->visible(['couponsid']); $row->visible(['stime']); $row->getRelation('stime')->visible(['time']); }
最后在對應的js里面加入對應的字段
{field: 'addrschool2.addrname', title: '下車地址'},
即可