model = new AttachmentModel(); } /** * 删除 * @param array $ids */ public function del(array $ids = []) { if (!$this->request->isDelete() || !$ids) { $this->error(__('Parameter error')); } $dataLimitAdminIds = $this->getDataLimitAdminIds(); if ($dataLimitAdminIds) { $this->model->where($this->dataLimitField, 'in', $dataLimitAdminIds); } $pk = $this->model->getPk(); $data = $this->model->where($pk, 'in', $ids)->select(); $count = 0; try { foreach ($data as $v) { Event::trigger('AttachmentDel', $v); $filePath = path_transform(public_path() . ltrim($v->url, '/')); if (file_exists($filePath)) { unlink($filePath); del_empty_dir(dirname($filePath)); } $count += $v->delete(); } } catch (PDOException|Exception $e) { $this->error(__('%d records and files have been deleted', [$count]) . $e->getMessage()); } if ($count) { $this->success(__('%d records and files have been deleted', [$count])); } else { $this->error(__('No rows were deleted')); } } }