Php 致命错误:在文件字段更新时对非对象调用成员函数saveAs()
我正在处理Yii上的一个文件字段。在create上,它工作得很好,但在更新上我遇到了麻烦 假设有人在filefield上保存了一个表单名city和一个图像,但忘记了输入类型,所以更新表单,编辑类型并再次保存,问题就出在这里。保存更新时,文件字段将删除以前上载的图像。我找到一些代码,但现在不是保存和删除图像,而是向我发送以下错误: 致命错误:对非对象调用成员函数saveAs() 这是我的表格:Php 致命错误:在文件字段更新时对非对象调用成员函数saveAs(),php,yii,Php,Yii,我正在处理Yii上的一个文件字段。在create上,它工作得很好,但在更新上我遇到了麻烦 假设有人在filefield上保存了一个表单名city和一个图像,但忘记了输入类型,所以更新表单,编辑类型并再次保存,问题就出在这里。保存更新时,文件字段将删除以前上载的图像。我找到一些代码,但现在不是保存和删除图像,而是向我发送以下错误: 致命错误:对非对象调用成员函数saveAs() 这是我的表格: <?php Yii::app()->user->setFlash('success'
<?php
Yii::app()->user->setFlash('success', '<strong>Well done!</strong> You successfully read this important alert message.');
$this->widget('bootstrap.widgets.TbAlert', array(
'block'=>true, // display a larger alert block?
'fade'=>true, // use transitions?
'closeText'=>'×', // close link text - if set to false, no close link is displayed
'alerts'=>array( // configurations per alert type
'error'=>array('block'=>true, 'fade'=>true, 'closeText'=>'×'), // success, info, warning, error or danger
),
));
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'reportes-form',
'enableAjaxValidation'=>false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)); ?>
<?php echo $form->errorSummary($model); ?>
<p class="help-block">Fields with <span class="required">*</span> are required.</p>
<div class="row">
<?php echo $form->labelEx($model, 'name'); ?>
<?php echo $form->textField($model,'name'); ?>
<?php echo $form->error($model, 'name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'city'); ?>
<?php echo $form->textField($model,'city'); ?>
<?php echo $form->error($model, 'city'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'id_type'); ?>
<?php $datos = CHtml::listData(Types::model()->findAll(array('condition'=>'', 'order'=>'')), 'id', 'name');
echo CHtml::activeDropDownList($model,'id_type',$datos,array('prompt'=>'Select', 'disabled'=> ''));
?>
</div>
<div class="row" >
<?php echo $form->labelEx($model, 'description'); ?>
<?php echo $form->textArea($model,'description'); ?>
<?php echo $form->error($model, 'description'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'image'); ?>
<?php if ($model->image): ?>
<div>Existing image: <?php echo CHtml::encode($model->image); ?></div>
<div>
<img src="<?php echo Yii::app()->request->baseUrl.'/images/uploads/'.$model->image?>" width="180" height="180" />
</div>
<?php endif; ?>
<?php echo $form->fileField($model, 'image'); ?>
<?php echo $form->error($model, 'image'); ?>
</div>
<div class="form-actions">
<?php $this->widget('bootstrap.widgets.TbButton', array(
'buttonType'=>'submit',
'type'=>'primary',
'label'=>$model->isNewRecord ? 'Create' : 'Save',
)); ?>
</div>
<?php $this->endWidget(); ?>
对$model->file
进行var_dump
会向我发送null
,并且$oldFileName
会在第一次上载图像的名称。所以我猜这应该可以省去老名字的麻烦。我缺少什么?(在评论和编辑中回答。请参阅)
@马丁·科马拉写道:
我认为很明显,如果
$model->image
为null
(由于条件的原因,它是空的),则不能调用$model->image->saveAs
。你想达到什么目标?是否要删除以前上载的文件
查看此wiki:
您需要更新您的模型,即,
$model->image='path to image'$模型->保存()代码>
OP写道:
我的更新控制器现在工作正常:
我认为很明显,您不能调用$model->image->saveAs,就好像$model->image为空一样(因为条件的原因,它是空的)。你想达到什么目标?你想删除之前上传的文件吗?哦,现在我明白了,查看这个维基:嘿,谢谢!这有一点帮助,现在,图像正在更新,但仅在分配的url中,它在数据库中保留旧图像。您需要更新您的模型,即$model->image='path to image'$模型->保存();
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Restaurants']))
{
$model->attributes=$_POST['Restaurants'];
$oldFileName = $_POST['Restaurants']['image'];
$model->image= CUploadedFile::getInstanceByName('Restaurants[image]');
$path = Yii::getPathOfAlias('webroot')."/images/uploads/";
if($model->save()) {
if($model->image == null){
$model->image->saveAs(Yii::getPathOfAlias('webroot')."/images/uploads/".$oldFileName);
$this->redirect(array('view','id'=>$model->id));
}
else
{
$model->image->saveAs(Yii::getPathOfAlias('webroot')."/images/uploads/".$model->image);
chmod( $path.$model->image, 0777 );
$this->redirect(array('view','id'=>$model->id));
}
}
}
$this->render('update',array(
'model'=>$model,
));
}
public function actionUpdate($id)
{
$model=$this->loadModel($id);
if(isset($_POST['Restaurants']))
{
$_POST['Restaurants']['image'] = $model->image;
$model->attributes=$_POST['Restaurants'];
$uploadedFile=CUploadedFile::getInstance($model,'image');
if($model->save())
{
if(!empty($uploadedFile)) // check if uploaded file is set or not
{
$uploadedFile->saveAs(Yii::getPathOfAlias('webroot')."/images/uploads/".$model->image);
}
$this->redirect(array('view','id'=>$model->id));
}
}
$this->render('update',array(
'model'=>$model,
));
}