Yii2 FileInput - Trying to get property of non obj

2019-08-11 16:15发布

问题:

i am using Yii2 and Kartik's FileInput extension and I have successfully get the file uploads working(only single upload). My problem now is that, I get the error as in the title(with logs attached) if I did not choose any files(It should be optional).

After much searching over the internet, I think it has to be something to do with array, but I am not sure how to fix that, especially even with the logs pointing to the exact line!

Here is my log,

Here is my model,

namespace app\models;

use Yii;

class FormMovement extends \yii\db\ActiveRecord
{

    public $file;

    public static function tableName()
    {
        return 'form_movement';
    }

    public function rules()
    {
        return [
            [['fm_date_received', 'fm_form_name', 'fm_from', 'fm_ptj'], 'required'],
            [['form_id'], 'integer'],
            [['fm_date_received', 'fm_date_action1', 'fm_date_action2','fm_upload'], 'safe'],
            [['fm_form_name', 'fm_note'], 'string', 'max' => 500],
            [['fm_from', 'fm_ptj', 'fm_action1', 'fm_action2'], 'string', 'max' => 100],
            [['file'], 'file', 'skipOnEmpty' => true, 'extensions'=>'jpg,pdf,png,doc,docx,xls,xlsx, jpeg', 'maxFiles' => 3],
        ];
    }

My controller function, the log shows that it is at the line 75, which is this one,

$model->fm_upload='uploads/'.$fileName.'.'.$model->file->extension;

Been tinkering with it, but no success.

public function actionCreate()
    {
        $model = new FormMovement();

        if ($model->load(Yii::$app->request->post())) {

            //set the file name
            $fileName = $model -> fm_form_name;

            //get instance
            $model->file = UploadedFile :: getInstance($model, 'file');

            //set the file path in the db
            $model->fm_upload='uploads/'.$fileName.'.'.$model->file->extension;

            //save the file to the server directory
            $model->save();
            $model->file->saveAs('uploads/'.$fileName.'.'.$model->file->extension);

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

Finally my view,

<div class="form-group kv-fieldset-inline">
    <?= Html::activeLabel($model, 'file[]', [
            'label'=>'MUAT NAIK FAIL',
            'class'=>'col-sm-1 control-label'
        ]) ?>

    <div class="col-sm-8">
    <?= $form->field($model, 'file',[
                'showLabels'=>false
            ])->widget(FileInput::classname(), [
            'options' => ['accept' => 'file/*', 'multiple' => 'true'],
            'pluginOptions'=>[
                'showUpload' => false,
            ]
    ]) ?>
    </div>
</div>

回答1:

This part should be refactored:

//set the file name
$fileName = $model -> fm_form_name;

//get instance
$model->file = UploadedFile :: getInstance($model, 'file');

//set the file path in the db
$model->fm_upload='uploads/'.$fileName.'.'.$model->file->extension;

//save the file to the server directory
$model->save();
$model->file->saveAs('uploads/'.$fileName.'.'.$model->file->extension);

like this:

$model->file = UploadedFile::getInstance($model, 'file');
$model->save();

if ($model->file) {    
    $model->fm_upload = "uploads/{$model->fm_form_name}.{$model->file->extension}";    
    $model->file->saveAs("uploads/{$model->fm_form_name}.{$model->file->extension}");
}

Also note that you don't handle failed validation in your controller at all.

For further refactoring, this line:

$model->file = UploadedFile::getInstance($model, 'file');

can be moved to beforeValidate() event handler.

This part:

if ($model->file) {    
    $model->fm_upload = "uploads/{$model->fm_form_name}.{$model->file->extension}";    
    $model->file->saveAs("uploads/{$model->fm_form_name}.{$model->file->extension}");
}

can be moved to afterSave() event handler to keep your controller slim.

In saveAs() it's better to use alias, I desribed it in this answer.