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