Yii2 FileInput - Trying to get property of non object

佐手、 提交于 2019-12-14 02:43:47

问题


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.



来源:https://stackoverflow.com/questions/34807059/yii2-fileinput-trying-to-get-property-of-non-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!