wah saya memantau dr kmren blm solved yah, maaf nih dr kmren mau kasih tau tp nunggu master2. berhubung blm ada yang kasih masukan…
saya coba kasih masukan mungkin gak terbaik,
actionUpdate di controller
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model);
if(isset($_POST['Item']))
{
if(isset($_POST['Item']['img']))
{
$model->attributes=$_POST['Item'];
$sss;
if(strlen(trim(CUploadedFile::getInstance($model,'img'))) > 0)
{
$sss=CUploadedFile::getInstance($model,'img');
$model->img=$model->brand.'.'.$sss->extensionName;
}
if($model->save())
{
if(strlen(trim($model->img)) > 0)
{
$sss->saveAs(Yii::app()->basePath . '/../images/item/'.$model->img);
}
$this->redirect(array('view','id'=>$model->id_item));
}
}
else
{
$model->attributes=$_POST['Item'];
if($model->save())
{
$this->redirect(array('view','id'=>$model->id_item));
}
}
}
$this->render('update',array(
'model'=>$model,));
}
Rules di Model, field img di allow empty (kalo mau bisa update atau create tanpa image di bolehin ya)
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('id_type, brand, desc, price, qty, status', 'required'),
array('id_type, price, qty, status', 'numerical', 'integerOnly'=>true),
array('brand', 'length', 'max'=>255),
array('img', 'file','allowEmpty' => true, 'types'=>'jpg, gif, png'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id_item, id_type, brand, desc, price, qty, status', 'safe', 'on'=>'search'),
);
}
nah terus kalo mau ganti Gambar ane pake algorith ane sendiri jadi ane buat fungsi 1 lagi, yaitu actionDeleteImg yang ane taro di controller, dan di view nya ane buat button nya untuk jalanin fungsi nya.
ini _form nya
<!--FORM jika suatu DATA ada imagenya akan ditampilkan ini -->
<div class="row">
<?php
if(!empty($model->img))
{
echo CHtml::link('Change image',array('deleteImg', 'id'=>$model->id_item));//inibuttonnya
$this->widget('zii.widgets.CDetailView',
array(
'data'=>$model,
'attributes'=>
array(
array(
'label'=>'Current Image',
'type'=>'raw',
'value'=>html_entity_decode(CHtml::image(Yii::app()->baseUrl . '/images/item/'.$model->img,'alt',array('width'=>'300'))),
),
),
));
}
else
{
?>
<div class="row">
<?php echo $form->labelEx($model,'img'); ?>
<?php echo $form->fileField($model,'img'); ?>
<?php echo $form->error($model,'img'); ?>
</div>
<?php
}
?>
?>
</div>
ini fungsi di controller untuk delete dan unlink imagenya
public function actionDeleteImg($id)
{
$model=$this->loadModel($id);
if(!empty($model->img))
{
unlink(Yii::app()->basePath . '/../images/item/'.$model->img);
$model->img ='';
if($model->save(false))
{
$this->redirect(array('update','id'=>$model->id_item));
}
}
else
{
$this->redirect(array('update','id'=>$model->id_item));
}
}
?>