File: /home/aliazzsr/api.crm.vqode.com/components/actions/PatchAction.php
<?php
namespace app\components\actions;
use yii\db\ActiveRecord;
use yii\rest\Action;
use yii\web\ServerErrorHttpException;
use yii\base\Model;
class PatchAction extends Action
{
/**
* @var string the scenario to be assigned to the model before it is validated and updated.
*/
public $scenario = Model::SCENARIO_DEFAULT;
/**
* @param $id
* @return ActiveRecord
* @throws ServerErrorHttpException
* @throws \yii\base\InvalidConfigException
* @throws \yii\web\NotFoundHttpException
*/
public function run($id)
{
/* @var $model ActiveRecord */
$model = $this->findModel($id);
if ($this->checkAccess) {
call_user_func($this->checkAccess, $this->id, $model);
}
$model->scenario = $this->scenario;
$params = \Yii::$app->getRequest()->getBodyParams();
$model->load($params, '');
if (!$model->validate(array_keys($params))) {
return $model;
}
if ($model->save(false) === false && !$model->hasErrors()) {
throw new ServerErrorHttpException('Failed to update the object for unknown reason.');
}
return $model;
}
}