phpdatabasefile-uploadyii2multi-upload

Multi file upload + textInput - doesn't save in db (yii2)


I am using yii2 basic and wanted to make a gallery, so I made use of multi file upload. All working fine, till I added a text input ('year').

GalleryController.php

    public function actionCreate()
    {
        $model = new MultipleUploadForm();
        $year;

        if (Yii::$app->request->isPost) {
            $model->files = UploadedFile::getInstances($model, 'files');
            if ($model->gaUpload()) {
                return $this->redirect(['index']);
            }
        }

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

MultipleUploadForm :

class MultipleUploadForm extends Model
{
    /**
     * @var UploadedFile[] files uploaded
     */
    public $files;
    public $year;

    public function rules()
    {
        return [
            [['files'], 'file', 'skipOnEmpty' => false, 'maxFiles' => 0],
            [['year'], 'string'],
        ];
    }
    public function gaUpload()
    {   
        if ($this->validate()) {
            foreach ($this->files as $file) {
                $model2 = new Gallery();
                $model2->img = $file->baseName . '_' . rand(100,999) . '.' . $file->extension;
                $model2->save_dir = 'uploads/gallery/';
                $model2->year = $this->year;
                $file->saveAs($model2->save_dir . $model2->img);

                $model2->save();

            }

            return true;
        } else {
            return false;
        }

    }
}

Gallery:

class Gallery extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'cgallery';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['year', 'img', 'save_dir'], 'required'],
            [['img', 'save_dir', 'year'], 'string', 'max' => 1024],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'year' => 'Year',
            'img' => 'Img',
            'save_dir' => 'Save Dir',
        ];
    }
}

_form.php:

    <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data'], ]); ?>

    <?= $form->field($model, 'year')->textInput()?>

    <?php
        echo '<label class="control-label">Add Pictures</label>';
        echo FileInput::widget([
            'model' => $model,
            'attribute' => 'files[]',
            'pluginOptions' => [
                'showRemove' => false,
                'uploadLabel' => 'Save',
                'uploadIcon' => '',
                'browseLabel' => '',
                'fileExtensions' => 'any',
            ],
            'options' => ['multiple' => true]
        ]);
    ?>

    <?php ActiveForm::end(); ?>

If I take 'year' out of the process, like : $model2->year = '2010' it work's just fine. I might need to add, that the files get uploaded (into the dir), but it won't get saved to the database.

Has anyone an idea what i did wrong?

EDIT:

Thanks to Wynton Franklin for the help. The solution was to add the line:

$model->load(\Yii::$app->request->post());

Changed GalleryController.php:

    public function actionCreate()
    {
        $model = new MultipleUploadForm();
        $model->load(\Yii::$app->request->post());

        if (Yii::$app->request->isPost) {    
            $model->files = UploadedFile::getInstances($model, 'files');
            if ($model->gaUpload()) {
                return $this->redirect(['index']);
            }
        }

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

Solution

  • In your controller year is not begin set. Check that.

    if (Yii::$app->request->isPost) {
                $model->year = $_POST[][] // or however you do it in yii
                $model->files = UploadedFile::getInstances($model, 'files');
                if ($model->gaUpload()) {
                    return $this->redirect(['index']);
                }
            }