मैं yii और php के लिए नया हूं। मैं एक फाइल अपलोड करना चाहता हूं और डेटाबेस में अपना रास्ता सहेजना चाहता हूं लेकिन ऐसा करने पर मुझे एक त्रुटि मिली।गैर-ऑब्जेक्ट पर किसी सदस्य फ़ंक्शन सेव() को कॉल करें [वाईआई 2]
मेरे नियंत्रक वर्ग है:
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,
]);
}
}
मेरे डेटाबेस स्तंभ नाम जहां मैं अपने फ़ाइल पथ बचाने "FilePath" है। मेरा विचार फ़ाइल है:
<?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>
मेरे नियमों और गुण हैं:
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',
];
}
अब मैं पहले से ही एक ही प्रश्न के लिए इस साइट का उल्लेख है, लेकिन मैं बनाने के लिए अद्यतन के लिए इसे नहीं पाते। कृपया मेरी मदद करो। जब मैं चलाने की कोशिश करता हूं मुझे त्रुटि मिली एक गैर-ऑब्जेक्ट पर किसी सदस्य फ़ंक्शन सेव() पर कॉल करें, मुझे नहीं पता कि मैं कहां गलत हूं।
वास्तव में क्या $ मॉडल> फ़ाइल है किया जाना चाहिए? var_dump इसे बचाने से पहले कृपया() –
कृपया व्यू व्यू (या _form) कोड भी प्रदान करें। मैं यह जांचना चाहता हूं कि उत्तर – scaisEdge
@scaisEdge मैंने अपनी पोस्ट संपादित करने से पहले फ़ाइल नाम कहां प्राप्त किया है, कृपया इसे देखें। –