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()
//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 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.
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.