AttributeValue.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace app\admin\controller\shop;
  3. use app\common\controller\Backend;
  4. use Exception;
  5. use think\Db;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. /**
  9. * 商品属性值
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class AttributeValue extends Backend
  14. {
  15. /**
  16. * AttributeValue模型对象
  17. * @var \app\admin\model\shop\AttributeValue
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\shop\AttributeValue;
  24. }
  25. /**
  26. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  27. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  28. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  29. */
  30. public function index()
  31. {
  32. $attribute_id = $this->request->param('attribute_id');
  33. $this->assignconfig('attribute_id', $attribute_id);
  34. return parent::index();
  35. }
  36. /**
  37. * 添加
  38. */
  39. public function add()
  40. {
  41. if ($this->request->isPost()) {
  42. $params = $this->request->post("row/a");
  43. $params['attribute_id'] = $this->request->param('attribute_id');
  44. if ($params) {
  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(true)->validate($validate);
  57. }
  58. $result = $this->model->allowField(true)->save($params);
  59. Db::commit();
  60. } catch (ValidateException $e) {
  61. Db::rollback();
  62. $this->error($e->getMessage());
  63. } catch (PDOException $e) {
  64. Db::rollback();
  65. $this->error($e->getMessage());
  66. } catch (Exception $e) {
  67. Db::rollback();
  68. $this->error($e->getMessage());
  69. }
  70. if ($result !== false) {
  71. $this->success();
  72. } else {
  73. $this->error(__('No rows were inserted'));
  74. }
  75. }
  76. $this->error(__('Parameter %s can not be empty', ''));
  77. }
  78. return $this->view->fetch();
  79. }
  80. }