rosey先生

娜娜老公很勤快!!!

Fastadmin 后台表单,外键关键,步骤

1.在 public\assets\js\backend 目录中找到对应的js,修改

2.对应控制器中加上index()方法;开启关联查询

重点:

protected $relationSearch = true; //开启关联查询

public function index()
{

//设置过滤方法
$this->request->filter(['strip_tags']);
//当前是否为关联查询
$this->relationSearch = true; // 解决排序问题
if ($this->request->isAjax()) {
//如果发送的来源是Selectpage,则转发到Selectpage
if ($this->request->request('keyField')) {
return $this->selectpage();
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$total = $this->model
->with(['choice'])
->where($where)
->order($sort, $order)
->count();

$list = $this->model
->with(['choice'])
->where($where)
->order($sort, $order)
->limit($offset, $limit)
->select();

$list = collection($list)->toArray();
$result = array("total" => $total, "rows" => $list);

return json($result);
}
return $this->view->fetch();
}

3.对应模型中model 中加上关联的方法

public function choice()
{
return $this->belongsTo('Choices', 'dw_id', 'id', [], 'LEFT')->setEagerlyType(0);   // Choices ------- 需要关联的模型  ;  dw_id ----- 外键id ; id ------ 关联表的id
}

 

4. 最终效果图

5.最后还有些小问题,为什么做了以上的步骤之后,只显示字段名,最后一步,同步语言包,把需要用的语言同步即可

 

 

 

详细文档,可查

https://doc.fastadmin.net/docs/controller.html#关联查询-5
posted @ 2018-10-18 14:43  rose_Y  阅读(4199)  评论(0编辑  收藏  举报