Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call to a member function saveAs() on a non-object[Yii 2]

Tags:

php

yii

yii2

I am new to yii and php. I want to upload a file and save its path to database but while doing so I got an error.

My controller class is:

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

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            $fileName = $model->name;
            $model->file =UploadedFile::getInstance($model,'file');
            $model->file->saveAs('uploadQuiz/'.$fileName.'.'.$model->file->extension );
            $model->filePath = 'uploadQuiz/'.$fileName.'.'.$model->file->extension ;
            $model->save();
            return $this->redirect(['view', 'id' => $model->idQuiz]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);

        }
    }

My database column name where I save my file path is "filePath". My view file is:

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $model app\models\Quiz */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="quiz-form">

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

    <?= $form->field($model, 'Course_idCourse')->textInput(['maxlength' => 100]) ?>

    <?= $form->field($model, 'name')->textInput(['maxlength' => 100]) ?>

    <?= $form->field($model, 'description')->textInput(['maxlength' => 255]) ?>

    <?= $form->field($model, 'duration')->textInput(['maxlength' => 100]) ?>

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

    <?= $form->field($model, 'file')->fileInput();  ?>

    <?= $form->field($model, 'totalMarks')->textInput(['maxlength' => 100]) ?>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

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

</div>

My rules and attributes are:

public function rules()
    {
        return [
            [['Course_idCourse', 'duration', 'time'], 'required'],
            [['Course_idCourse', 'duration', 'totalMarks'], 'integer'],
            [['time'], 'safe'],
            [['file'],'file'],
            [['name', 'filePath'], 'string', 'max' => 200],
            [['description'], 'string', 'max' => 255]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'idQuiz' => 'Id Quiz',
            'Course_idCourse' => 'Course Id Course',
            'name' => 'Name',
            'description' => 'Description',
            'duration' => 'Duration',
            'time' => 'Time',
            'file' => 'Quiz ',
            'totalMarks' => 'Total Marks',
        ];
    }

Now I already refer this site for the same question but I find it for update not for create . kINDLY HELP ME. When I run try to create I got an error Call to a member function saveAs() on a non-object I don't figure where am I going wrong.

like image 852
John Sick Avatar asked Aug 22 '15 07:08

John Sick


1 Answers

No file is being uploaded. The option parameter in the ActiveForm initialization should be options

<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
like image 61
topher Avatar answered Sep 20 '22 17:09

topher