UniversityCourseChapter.php 2.6 KB

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