Issues (13)

src/actions/CreateAction.php (1 issue)

Labels
Severity
1
<?php
2
3
4
namespace carono\yii2crud\actions;
5
6
7
use carono\yii2crud\CrudController;
8
use carono\yii2crud\Event;
9
use Yii;
10
use yii\db\ActiveRecord;
11
use yii\helpers\Html;
12
13
/**
14
 * Class CreateAction
15
 *
16
 * @package carono\yii2crud\actions
17
 * @property CrudController $controller
18
 * @method getMessageOnCreate(\yii\db\ActiveRecord $model)
19
 */
20
class CreateAction extends Action
21
{
22
    public $view = 'create';
23
    public $loadDefaultValues = true;
24
    public $loadGetParams = true;
25
    public $skipIfSet = true;
26
    public $messageOnCreate = 'Model Successful Created';
27
28
    public function run()
29
    {
30
        /**
31
         * @var ActiveRecord $class
32
         * @var ActiveRecord|Model $model
33
         */
34
        $class = $this->modelClass ?: ($this->controller->createClass ?: $this->controller->modelClass);
35
        $model = new $class();
36
        $this->controller->trigger(CrudController::EVENT_BEFORE_CREATE, new Event(['model' => $model]));
37
        if ($this->loadDefaultValues && method_exists($model, 'loadDefaultValues')) {
38
            $model->loadDefaultValues($this->skipIfSet);
39
        }
40
        if ($this->loadGetParams && method_exists($model, 'load')) {
41
            $model->load(\Yii::$app->request->get());
42
        }
43
        if ($model->load(Yii::$app->request->post())) {
44
            if ($model->save()) {
45
                Yii::$app->session->setFlash('success', $this->getMessageOnCreate($model));
0 ignored issues
show
The method setFlash() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
                Yii::$app->session->/** @scrutinizer ignore-call */ 
46
                                    setFlash('success', $this->getMessageOnCreate($model));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
46
                $this->controller->trigger(CrudController::EVENT_AFTER_CREATE, new Event(['model' => $model]));
47
                return $this->controller->redirect($this->controller->createRedirect($model));
48
            } else {
49
                $this->controller->trigger(CrudController::EVENT_ERROR_CREATE, new Event(['model' => $model]));
50
            }
51
            Yii::$app->session->setFlash('error', Html::errorSummary($model));
52
        }
53
        return $this->controller->render($this->view, ['model' => $model]);
54
    }
55
}