Yii afterDelete()

Need help asp.

Hi to all, I have a model for Items. I this model I have a relationship to another model Images where I save the path to the item images in which an item can have as many images.

When an item is deleted I need to delete the path in the image table and this works fine but I need to delete the file(image) of the item too. Here is my code but is not deleting the file which is the image using the unlink function. I might be missing something please help.

 protected function afterDelete()



    ItemRecommendations::model()->deleteAll('item_id=' .$this->id);

    ItemTag::model()->updateFrequency($this->tags, '');


    //Delete images location

    $image_path = Yii::app()->getBasePath().'/../../uploads/item/';


    //query table where id = current id

    $item_data = ItemImage::model()->findAll('item_id =' .$this->id);





    //Delete all the image path for the current item

    ItemImage::model()->deleteAll('item_id =' .$this->id);


I’m surprised you aren’t getting an error.

Is wrong, the findAll() method returns an array, so even if there is only one result you would need to:

$item_data = ItemImage::model()->findAll('item_id =' .$this->id);

foreach ($item_data as $val) {

  if (!empty($val->image_path)) {





Hope this helped, good luck.

Thanks luke I will try your solution and see if it works.

I tried your method the only thing it does is deleting the value of the image path in the database but not the image file from the folder am uploading the images. I need it to delete the item image from the folder am uploading.

That’s identical to what I do - are you absolutely sure your paths are right? you’re going relative … so that might be a culprit. I would prefer doing something ( in my top level index )

Yii::app()->params[‘webroot’] = getcwd();

so you can do ( if you’re images are held in the web root )

Yii::app()->params[‘webroot’] . ‘/’ . $pathToImage;

I try to avoid any number of periods at any point I can.

Thanks notsoluckycharm for your help.I have my upload folder at the same level as yii default index.php. The folder is in the format uploads/items and in my image table I have as example for image_path column I have upload/items/image1.jpg and for the thumb I have image_thumb_path upload/items/image1_thumb.jpg. Also what is getcwd() in your example. Thinks seem not to work. The path is deleted from the table fine but the image is not being removed from the folder.

Thanks everybody I got it working but I used beforeDelete(). Also I made changes to my base path ‘/../../’


Bonnie, can you show me your complete code (model, controller) to delete file because I have the same problem. To remove I use standard solutions of yii.

For removing a single image

I have simply done this way assuming that am the current logged in user and I want to delete my avatar

Here the form that shows my avatar and delete button




echo CHtml::image(Yii::app()->request->baseUrl.'/'.$model->image_thumb_path);




echo CHtml::image(Yii::app()->request->baseUrl.'/uploads/user/noavatar.png');


echo CHtml::beginForm()?>

<?php echo CHtml::Button(Yii::t('lan','Remove Avatar'), array(

'submit' => array(

'//user/removeAvatar')));; ?>

And this the controller that does the delete.

public function actionRemoveAvatar() 


$model = User::model()->findByPk(Yii::app()->user->id);

$model->image_path = '';

$model->image_thumb_path = '';

$image_path = Yii::app()->getBasePath().'/../../';






The image_thumb_path and image_path are the field in the user table that stores the url to images for different sizes.

I think you need to do unlink in your ItemImage controller delete.It will help you