V
V
Viktor Yanyshev2017-10-15 15:20:44
Yii
Viktor Yanyshev, 2017-10-15 15:20:44

Why is the upload file not saved?

When I create a new record, it is not saved if I still upload the file. As a result, there is no record id to which to redirect, if you exclude working with files, then the record is created and sent to the view view.
This is what the model looks like:

class Journal extends \yii\db\ActiveRecord
{

    const SCENARIO_CREATE = 'create';
    const SCENARIO_UPDATE = 'update';

    public function scenarios()
    {
        $scenarios = parent::scenarios();
        $scenarios[self::SCENARIO_CREATE] = ['title', 'preview'];
        $scenarios[self::SCENARIO_UPDATE] = ['title'];
        return $scenarios;
    }

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'journal';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['title', 'preview'], 'required', 'on' => self::SCENARIO_CREATE],
            [['title'], 'required', 'on' => self::SCENARIO_UPDATE],
            [['content'], 'string'],
            [['created_at', 'updated_at'], 'safe'],
            [['title', 'preview'], 'string', 'max' => 150],
            [['description'], 'string', 'max' => 250],
            [['url'], 'string', 'max' => 70],
            [['preview'], 'file', 'extensions' => 'png, jpg, jpeg, gif', 'skipOnEmpty' => true]
        ];
    }
....

Controller method for creating a new entry:
public function actionCreate()
    {
        $this->view->title = 'Создание статьи';
        $this->view->params['breadcrumbs'][] = ['label' => 'Журнал', 'url' => ['panel/journal']];
        $this->view->params['breadcrumbs'][] = $this->view->title;

        $model = new Journal(['scenario' => Journal::SCENARIO_CREATE]);

        if ($model->load(Yii::$app->request->post())) {
            $model->content = Html::decode($model->content);
            $model->url = Translit::t($model->title);
            $model->preview = UploadedFile::getInstance($model, 'preview');

            $path = Yii::$app->params['uploads_path'].'/journal/preview/';
            $model->preview->saveAs($path.$model->preview->name);

            $model->save();

            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
B
Boris Korobkov, 2017-10-15
@BorisKorobkov

if ($model->load(...)) {
    ...
    if ($model->save()) {
        return $this->redirect(['view', 'id' => $model->id]);
    } 
}

return $this->render('create', [
    'model' => $model,
]);

P
Pavel Gogolinsky, 2017-10-15
@gogolinsky

Do a VarDumper::dump($model->getErrors(), 10, true) after trying to save, see the errors

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question