Yii2 FileInput - 尝试获取非 object 的 属性
Yii2 FileInput - Trying to get property of non object
我正在使用 Yii2 和 Kartik 的 FileInput 扩展,我已经成功地让文件上传正常工作(仅单次上传)。我现在的问题是,如果我没有选择任何文件(它应该是可选的),我会得到标题中的错误(附有日志)。
在网上搜索了很多次之后,我认为这一定与数组有关,但我不确定如何解决这个问题,尤其是即使日志指向确切的行!
这是我的日志,
这是我的模型,
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],
];
}
我的controller函数,log显示在第75行,就是这个,
$model->fm_upload='uploads/'.$fileName.'.'.$model->file->extension;
一直在修改,但没有成功。
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,
]);
}
}
最后是我的看法,
<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>
这部分应该重构:
//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);
像这样:
$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}");
}
另请注意,您根本不会在控制器中处理失败的验证。
为了进一步重构,这一行:
$model->file = UploadedFile::getInstance($model, 'file');
可以移动到 beforeValidate()
事件处理程序。
这部分:
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}");
}
可以移动到 afterSave()
事件处理程序以使您的控制器保持纤薄。
在saveAs()
中最好使用别名,我在answer中描述了它。
我正在使用 Yii2 和 Kartik 的 FileInput 扩展,我已经成功地让文件上传正常工作(仅单次上传)。我现在的问题是,如果我没有选择任何文件(它应该是可选的),我会得到标题中的错误(附有日志)。
在网上搜索了很多次之后,我认为这一定与数组有关,但我不确定如何解决这个问题,尤其是即使日志指向确切的行!
这是我的日志,
这是我的模型,
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],
];
}
我的controller函数,log显示在第75行,就是这个,
$model->fm_upload='uploads/'.$fileName.'.'.$model->file->extension;
一直在修改,但没有成功。
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,
]);
}
}
最后是我的看法,
<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>
这部分应该重构:
//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);
像这样:
$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}");
}
另请注意,您根本不会在控制器中处理失败的验证。
为了进一步重构,这一行:
$model->file = UploadedFile::getInstance($model, 'file');
可以移动到 beforeValidate()
事件处理程序。
这部分:
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}");
}
可以移动到 afterSave()
事件处理程序以使您的控制器保持纤薄。
在saveAs()
中最好使用别名,我在answer中描述了它。