model = new BodyProfileModel; } /** * 默认生成的控制器所继承的父类中有index方法,在这里重写下 */ public function index() { //当前是否为关联查询 $this->relationSearch = true; //设置过滤方法 $this->request->filter(['strip_tags', 'trim']); if ($this->request->isAjax()) { //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('keyField')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $list = $this->model ->with(['user']) ->where($where) ->order($sort, $order) ->paginate($limit); foreach ($list as $row) { $row->visible(['id','profile_name','user_id','gender','is_own','relation','age','height','weight','createtime','updatetime']); $row->visible(['user']); $row->getRelation('user')->visible(['username','nickname']); // 计算BMI $row['bmi'] = $row->calculateBMI(); $row['bmi_level'] = $row->getBMILevel(); } $result = array("total" => $list->total(), "rows" => $list->items()); return json($result); } return $this->view->fetch(); } /** * 查看详情 */ public function detail($ids = null) { $row = $this->model->get($ids); if (!$row) { $this->error(__("No Results were found")); } $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) { $this->error(__("You have no permission")); } // 使用方法查询 $profileData = BodyProfileService::getProfileDetail($row->id, $row->user_id); $this->view->assign("profileData", $profileData); $this->view->assign("row", $row); return $this->view->fetch(); } }