/** * @param $id * @return ActiveRecord * @throws \yii\web\NotFoundHttpException */ public function run($id) { /** @var ActiveRecord $model */ $model = new $this->modelClass(); $model = $model->findOne(['id' => $id]); if (empty($model)) { HttpError::the404('Not found'); } return $model; }
/** * @param $id * @throws ServerErrorHttpException * @throws \Exception * @throws \yii\web\NotFoundHttpException */ public function run($id) { /** @var \yii\db\ActiveRecord $model */ $model = new $this->modelClass(); $model = $model->findOne(['id' => $id]); if (empty($model)) { HttpError::the404('Not found'); } if ($model->delete() === false) { throw new ServerErrorHttpException('Failed to delete the object for unknown reason.'); } Yii::$app->getResponse()->setStatusCode(204); }
/** * @param $id * @return \yii\db\ActiveRecord * @throws ServerErrorHttpException * @throws \yii\base\InvalidConfigException * @throws \yii\web\NotFoundHttpException */ public function run($id) { /** @var \yii\db\ActiveRecord $model */ $model = new $this->modelClass(); $model = $model->findOne(['id' => $id]); if (empty($model)) { HttpError::the404('Not found'); } $model->load(Yii::$app->getRequest()->getBodyParams(), ''); if ($model->save() === false && !$model->hasErrors()) { throw new ServerErrorHttpException('Failed to update the object for unknown reason.'); } return $model; }