CurdController.php 2.2 KB

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