UniversityEvent.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\exception\PDOException;
  6. use think\exception\ValidateException;
  7. /**
  8. * 【老年大学】活动
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class UniversityEvent extends Backend
  13. {
  14. /**
  15. * UniversityEvent模型对象
  16. * @var \app\admin\model\UniversityEvent
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\admin\model\UniversityEvent;
  23. $this->view->assign("statusList", $this->model->getStatusList());
  24. }
  25. /**
  26. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  27. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  28. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  29. */
  30. /**
  31. * 添加
  32. *
  33. * @return string
  34. * @throws \think\Exception
  35. */
  36. public function add()
  37. {
  38. if (false === $this->request->isPost()) {
  39. return $this->view->fetch();
  40. }
  41. $params = $this->request->post('row/a');
  42. if (empty($params)) {
  43. $this->error(__('Parameter %s can not be empty', ''));
  44. }
  45. $params = $this->preExcludeFields($params);
  46. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  47. $params[$this->dataLimitField] = $this->auth->id;
  48. }
  49. $result = false;
  50. Db::startTrans();
  51. try {
  52. //是否采用模型验证
  53. if ($this->modelValidate) {
  54. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  55. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  56. $this->model->validateFailException()->validate($validate);
  57. }
  58. $result = $this->model->allowField(true)->save($params);
  59. Db::name('university_event_wallet')->insert(['shop_id' =>$this->model->id]);
  60. Db::commit();
  61. } catch (ValidateException|PDOException|Exception $e) {
  62. Db::rollback();
  63. $this->error($e->getMessage());
  64. }
  65. if ($result === false) {
  66. $this->error(__('No rows were inserted'));
  67. }
  68. $this->success();
  69. }
  70. }