AttributeController.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace addons\TinyShop\merchant\modules\common\controllers;
  3. use Yii;
  4. use common\enums\StatusEnum;
  5. use common\models\base\SearchModel;
  6. use common\traits\MerchantCurd;
  7. use addons\TinyShop\common\models\common\Attribute;
  8. use addons\TinyShop\merchant\controllers\BaseController;
  9. use addons\TinyShop\merchant\modules\common\forms\AttributeForm;
  10. /**
  11. * 商品参数
  12. *
  13. * Class AttributeController
  14. * @package addons\TinyShop\merchant\modules\common\controllers
  15. * @author jianyan74 <751393839@qq.com>
  16. */
  17. class AttributeController extends BaseController
  18. {
  19. use MerchantCurd;
  20. /**
  21. * @var Attribute
  22. */
  23. public $modelClass = Attribute::class;
  24. /**
  25. * 首页
  26. *
  27. * @return string
  28. * @throws \yii\web\NotFoundHttpException
  29. */
  30. public function actionIndex()
  31. {
  32. $searchModel = new SearchModel([
  33. 'model' => Attribute::class,
  34. 'scenario' => 'default',
  35. 'partialMatchAttributes' => ['title'], // 模糊查询
  36. 'defaultOrder' => [
  37. 'sort' => SORT_ASC,
  38. 'id' => SORT_DESC,
  39. ],
  40. 'pageSize' => $this->pageSize,
  41. ]);
  42. $dataProvider = $searchModel
  43. ->search(Yii::$app->request->queryParams);
  44. $dataProvider->query
  45. ->andWhere(['status' => StatusEnum::ENABLED])
  46. ->andWhere(['merchant_id' => $this->getMerchantId()])
  47. ->with(['value']);
  48. return $this->render($this->action->id, [
  49. 'dataProvider' => $dataProvider,
  50. 'searchModel' => $searchModel,
  51. ]);
  52. }
  53. /**
  54. * 编辑/创建
  55. *
  56. * @return mixed
  57. */
  58. public function actionEdit()
  59. {
  60. $id = Yii::$app->request->get('id', null);
  61. $model = $this->findFormModel($id);
  62. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  63. return $this->referrer();
  64. }
  65. return $this->render($this->action->id, [
  66. 'model' => $model,
  67. ]);
  68. }
  69. /**
  70. * 返回模型
  71. *
  72. * @param $id
  73. * @return \yii\db\ActiveRecord
  74. */
  75. protected function findFormModel($id)
  76. {
  77. /* @var $model \yii\db\ActiveRecord */
  78. if (empty($id) || empty(($model = AttributeForm::findOne(['id' => $id, 'merchant_id' => $this->getMerchantId()])))) {
  79. $model = new AttributeForm();
  80. return $model->loadDefaultValues();
  81. }
  82. return $model;
  83. }
  84. }
粤ICP备19079148号