Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

53 lines
1.4KB

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\rest;
  8. use Yii;
  9. use yii\base\Model;
  10. use yii\db\ActiveRecord;
  11. use yii\web\ServerErrorHttpException;
  12. /**
  13. * UpdateAction implements the API endpoint for updating a model.
  14. *
  15. * @author Qiang Xue <qiang.xue@gmail.com>
  16. * @since 2.0
  17. */
  18. class UpdateAction extends Action
  19. {
  20. /**
  21. * @var string the scenario to be assigned to the model before it is validated and updated.
  22. */
  23. public $scenario = Model::SCENARIO_DEFAULT;
  24. /**
  25. * Updates an existing model.
  26. * @param string $id the primary key of the model.
  27. * @return \yii\db\ActiveRecordInterface the model being updated
  28. * @throws ServerErrorHttpException if there is any error when updating the model
  29. */
  30. public function run($id)
  31. {
  32. /* @var $model ActiveRecord */
  33. $model = $this->findModel($id);
  34. if ($this->checkAccess) {
  35. call_user_func($this->checkAccess, $this->id, $model);
  36. }
  37. $model->scenario = $this->scenario;
  38. $model->load(Yii::$app->getRequest()->getBodyParams(), '');
  39. if ($model->save() === false && !$model->hasErrors()) {
  40. throw new ServerErrorHttpException('Failed to update the object for unknown reason.');
  41. }
  42. return $model;
  43. }
  44. }