SwitcherAction.php 826 B

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. namespace backend\widgets\grid;
  3. use backend\actions\Action;
  4. use Yii;
  5. class SwitcherAction extends Action
  6. {
  7. public function run()
  8. {
  9. Yii::$app->response->format = 'json';
  10. $id = Yii::$app->request->post('id');
  11. $attribute = Yii::$app->request->post('attribute');
  12. $value = Yii::$app->request->post('value');
  13. $model = $this->findModel($id);
  14. $model->setAttribute($attribute,intval($value));
  15. $model->update();
  16. if(count($model->errors) >0)
  17. {
  18. $error = current($model->getFirstErrors());
  19. return [
  20. 'status' => false,
  21. 'msg' => $error
  22. ];
  23. } else {
  24. return [
  25. 'status' => true,
  26. 'msg' => '更新成功'
  27. ];
  28. }
  29. }
  30. }