fastadmin中表格中增加管理某个数据库的字段belongsTo

比如我有个数据库是地址表名为

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: '下车地址'},

即可

 

 

posted @ 2020-12-04 14:17  xiaohe520  阅读(1703)  评论(0编辑  收藏  举报