for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
/**
* Created by PhpStorm.
* Date: 2017-12-07
* Time: 03:12
*/
namespace MP\ExtendedApi;
use Yii;
use yii\rest\CreateAction;
use yii\base\Model;
use yii\helpers\Url;
use yii\web\ServerErrorHttpException;
* Class ECreateAction
* @package MP\ExtendedApi
* @author Yarmaliuk Mikhail
* @version 1.0
class ECreateAction extends CreateAction
{
* Creates a new model.
* @return \yii\db\ActiveRecordInterface the model newly created
* @throws ServerErrorHttpException if there is any error when creating the model
public function run()
/* @var $model \yii\db\ActiveRecord */
$model = new $this->modelClass([
'scenario' => $this->scenario,
]);
$model->load(Yii::$app->getRequest()->getBodyParams(), '');
if ($this->checkAccess) {
call_user_func($this->checkAccess, $this->id, $model);
}
if ($model->save()) {
$response = Yii::$app->getResponse();
$response->setStatusCode(201);
$id = implode(',', array_values($model->getPrimaryKey(true)));
$response->getHeaders()->set('Location', Url::toRoute([$this->viewAction, 'id' => $id], true));
} elseif (!$model->hasErrors()) {
throw new ServerErrorHttpException('Failed to create the object for unknown reason.');
return $model;