model = new \app\admin\model\attachment\AttachmentGroup; $this->attachmentModel = new \app\common\model\Attachment; } /** * 查看 */ public function index() { //当前是否为关联查询 $this->relationSearch = false; //设置过滤方法 $this->request->filter(['strip_tags', 'trim']); //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('keyField')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $limit = 10000; $list = $this->model ->where($where) ->order($sort, $order) ->paginate($limit); $result = array("total" => $list->total(), "rows" => $list->items()); return $this->success("获取成功",$result); } /** * 删除 * * @param $ids * @return void * @throws DbException * @throws DataNotFoundException * @throws ModelNotFoundException */ public function del($ids = null) { if (false === $this->request->isPost()) { $this->error(__("Invalid parameters")); } $ids = $ids ?: $this->request->post("ids"); if (empty($ids)) { $this->error(__('Parameter %s can not be empty', 'ids')); } $pk = $this->model->getPk(); $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds)) { $this->model->where($this->dataLimitField, 'in', $adminIds); } $allIds = $this->getAllChildId($ids); $list = $this->model->where($pk, 'in', $allIds)->select(); $count = 0; Db::startTrans(); try { foreach ($list as $item) { $count += $item->delete(); //重置文件分组id $this->attachmentModel->where(['attachment_group'=>$item->id])->update(['attachment_group'=>0]); } Db::commit(); } catch (PDOException|Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($count) { $this->success("操作成功"); } $this->error(__('No rows were deleted')); } protected $allChildIds = []; /** * 获取所有子分组 id * @param $ids * @return mixed */ public function getAllChildId($ids,$depth=0) { if($depth>2){ return $this->allChildIds; } if(!is_array($ids)){ $ids = [$ids]; } $this->allChildIds = array_merge($this->allChildIds, $ids); $childIds = $this->model->where('p_id', 'in', $ids)->column('id'); if(!$childIds){ return $this->allChildIds; } return $this->getAllChildId($childIds,++$depth); } }