Reputation: 41
I'm working with a FileField on Yii. On create it's working fine, but I'm having troubles on update.
Let's say that someone save a form name city and an image on filefield but forget to put the type, so UPDATE the form, edit the type and save it again, here is where the problem comes. At the moment of save the update, the filefield delete the image that have uploaded before. I find some codes, but now instead of saving and deleting the image, is sending me this error:
Fatal error: Call to a member function saveAs() on a non-object.
Here is my form:
<?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(); ?>
Here is my controller update:
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,
));
}
Making a var_dump
of $model->file
sends me null
, and $oldFileName
have the name of the image uploaded on the first time. So I'm guessing this should save on $model->image
the old name without trouble. What I'm missing?
Upvotes: 2
Views: 7642
Reputation: 5893
(Answered in the comments and edits. See Question with no answers, but issue solved in the comments (or extended in chat) )
@Martin Komara wrote:
I think it's quite obvious that you cannot call
$model->image->saveAs
if$model->image
isnull
(which it is, because of the condition). What are you trying to achieve? Do you want to delete the file that was uploaded previously?check this wiki: http://www.yiiframework.com/wiki/349/how-to-upload-image-photo-and-path-entry-in-datebase-with-update-functionality/
You need to update your model, i.e.
$model->image = 'path to image'; $model->save();
The OP wrote:
My update controller now working fine:
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,
));
}
Upvotes: 2