model = new \app\admin\model\service\Cat; $this->view->assign("statusList", $this->model->getStatusList()); } public function index() { $this->request->filter(['strip_tags', 'trim']); if (false === $this->request->isAjax()) { return $this->view->fetch(); } if ($this->request->request('keyField')) { return $this->selectpage(); } [$where, $sort, $order, $offset, $limit] = $this->buildparams(); $list = $this->model ->where($where) ->order($sort, $order) ->paginate($limit); $rows = $list->items(); if (!empty($rows)) { $catCountRows = Db::name('service') ->field('cat_id, count(id) as cnt') ->group('cat_id') ->select(); $catCounts = []; foreach ($catCountRows as $cr) { $catCounts[(int)$cr['cat_id']] = (int)$cr['cnt']; } foreach ($rows as &$r) { $cid = (int)$r['id']; $r['goods_count'] = isset($catCounts[$cid]) ? (int)$catCounts[$cid] : 0; } unset($r); } return json(['total' => $list->total(), 'rows' => $rows]); } /** * 删除保护:类目下仍有关联商品时禁止物理删除 */ public function del($ids = null) { if (false === $this->request->isPost()) { $this->error(__('Invalid parameters')); } $ids = $ids ?: $this->request->post('ids'); $idArr = array_values(array_filter(array_map('intval', explode(',', (string)$ids)))); if (!$idArr) { $this->error(__('Parameter %s can not be empty', 'ids')); } $cnt = (int)Db::name('service')->where('cat_id', 'in', $idArr)->count(); if ($cnt > 0) { $this->error('所选类目下仍包含 ' . $cnt . ' 种服务商品,请先迁移或删除下属商品后再操作'); } return parent::del($ids); } }