1 | <?php |
||||
2 | /** |
||||
3 | * @link https://www.yiiframework.com/ |
||||
4 | * @copyright Copyright (c) 2008 Yii Software LLC |
||||
5 | * @license https://www.yiiframework.com/license/ |
||||
6 | */ |
||||
7 | |||||
8 | namespace yii\rest; |
||||
9 | |||||
10 | use Yii; |
||||
11 | use yii\base\Model; |
||||
12 | use yii\db\ActiveRecord; |
||||
13 | use yii\web\ServerErrorHttpException; |
||||
14 | |||||
15 | /** |
||||
16 | * UpdateAction implements the API endpoint for updating a model. |
||||
17 | * |
||||
18 | * For more details and usage information on UpdateAction, see the [guide article on rest controllers](guide:rest-controllers). |
||||
19 | * |
||||
20 | * @author Qiang Xue <[email protected]> |
||||
21 | * @since 2.0 |
||||
22 | */ |
||||
23 | class UpdateAction extends Action |
||||
24 | { |
||||
25 | /** |
||||
26 | * @var string the scenario to be assigned to the model before it is validated and updated. |
||||
27 | */ |
||||
28 | public $scenario = Model::SCENARIO_DEFAULT; |
||||
29 | |||||
30 | |||||
31 | /** |
||||
32 | * Updates an existing model. |
||||
33 | * @param string $id the primary key of the model. |
||||
34 | * @return \yii\db\ActiveRecordInterface the model being updated |
||||
35 | * @throws ServerErrorHttpException if there is any error when updating the model |
||||
36 | */ |
||||
37 | public function run($id) |
||||
38 | { |
||||
39 | /* @var $model ActiveRecord */ |
||||
40 | $model = $this->findModel($id); |
||||
41 | |||||
42 | if ($this->checkAccess) { |
||||
43 | call_user_func($this->checkAccess, $this->id, $model); |
||||
44 | } |
||||
45 | |||||
46 | $model->scenario = $this->scenario; |
||||
47 | $model->load(Yii::$app->getRequest()->getBodyParams(), ''); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() It seems like
Yii::app->getRequest()->getBodyParams() can also be of type object ; however, parameter $data of yii\base\Model::load() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
48 | if ($model->save() === false && !$model->hasErrors()) { |
||||
49 | throw new ServerErrorHttpException('Failed to update the object for unknown reason.'); |
||||
50 | } |
||||
51 | |||||
52 | return $model; |
||||
53 | } |
||||
54 | } |
||||
55 |