Attachment.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace app\admin\controller\routine;
  3. use Throwable;
  4. use app\common\controller\Backend;
  5. use app\common\model\Attachment as AttachmentModel;
  6. class Attachment extends Backend
  7. {
  8. /**
  9. * @var object
  10. * @phpstan-var AttachmentModel
  11. */
  12. protected object $model;
  13. protected string|array $quickSearchField = 'name';
  14. protected array $withJoinTable = ['admin', 'user'];
  15. protected string|array $defaultSortField = 'last_upload_time,desc';
  16. public function initialize(): void
  17. {
  18. parent::initialize();
  19. $this->model = new AttachmentModel();
  20. }
  21. /**
  22. * 删除
  23. * @throws Throwable
  24. */
  25. public function del(): void
  26. {
  27. $where = [];
  28. $dataLimitAdminIds = $this->getDataLimitAdminIds();
  29. if ($dataLimitAdminIds) {
  30. $where[] = [$this->dataLimitField, 'in', $dataLimitAdminIds];
  31. }
  32. $ids = $this->request->param('ids/a', []);
  33. $where[] = [$this->model->getPk(), 'in', $ids];
  34. $data = $this->model->where($where)->select();
  35. $count = 0;
  36. try {
  37. foreach ($data as $v) {
  38. $count += $v->delete();
  39. }
  40. } catch (Throwable $e) {
  41. $this->error(__('%d records and files have been deleted', [$count]) . $e->getMessage());
  42. }
  43. if ($count) {
  44. $this->success(__('%d records and files have been deleted', [$count]));
  45. } else {
  46. $this->error(__('No rows were deleted'));
  47. }
  48. }
  49. }