Yii Framework Forum: Yii afterDelete() - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Yii afterDelete() Rate Topic: -----

#1 User is offline   bonnie 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 08-February 11

Posted 05 February 2012 - 06:01 PM

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()
    {
    parent::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);
   
    if(!empty($item_data->image_path))
    unlink($image_path.$item_data->image_path);
    unlink($image_path.$item_data->image_thumb_path);

    //Delete all the image path for the current item
    ItemImage::model()->deleteAll('item_id =' .$this->id);
    }

0

#2 User is offline   Luke Jurgs 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 162
  • Joined: 16-October 11
  • Location:Australia

Posted 05 February 2012 - 06:50 PM

View Postbonnie, on 05 February 2012 - 06:01 PM, said:

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()
    {
    parent::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);
   
    if(!empty($item_data->image_path))
    unlink($image_path.$item_data->image_path);
    unlink($image_path.$item_data->image_thumb_path);

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

Quote

    $item_data = ItemImage::model()->findAll('item_id =' .$this->id);
   
    if(!empty($item_data->image_path))
    unlink($image_path.$item_data->image_path);
    unlink($image_path.$item_data->image_thumb_path);



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)) {
    unlink($image_path.$val->image_path);
    unlink($image_path.$val->image_thumb_path);
  }
}


Hope this helped, good luck.
0

#3 User is offline   bonnie 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 08-February 11

Posted 06 February 2012 - 07:15 AM

View PostLuke Jurgs, on 05 February 2012 - 06:50 PM, said:

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)) {
    unlink($image_path.$val->image_path);
    unlink($image_path.$val->image_thumb_path);
  }
}


Hope this helped, good luck.


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

#4 User is offline   bonnie 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 08-February 11

Posted 06 February 2012 - 11:08 AM

View PostLuke Jurgs, on 05 February 2012 - 06:50 PM, said:

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)) {
    unlink($image_path.$val->image_path);
    unlink($image_path.$val->image_thumb_path);
  }
}


Hope this helped, good luck.


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.
0

#5 User is offline   notsoluckycharm 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 15-February 11

Posted 06 February 2012 - 02:49 PM

View Postbonnie, on 06 February 2012 - 11:08 AM, said:

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.
0

#6 User is offline   bonnie 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 08-February 11

Posted 08 February 2012 - 10:53 PM

View Postnotsoluckycharm, on 06 February 2012 - 02:49 PM, said:

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.
0

#7 User is offline   bonnie 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 08-February 11

Posted 13 February 2012 - 11:29 PM

View Postbonnie, on 08 February 2012 - 10:53 PM, said:

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 '/../../'
0

#8 User is offline   robikon 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 32
  • Joined: 17-October 11

Posted 05 April 2012 - 02:50 AM

Hi.
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.
0

#9 User is offline   bonnie 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 08-February 11

Posted 11 August 2012 - 11:49 AM

View Postrobikon, on 05 April 2012 - 02:50 AM, said:

Hi.
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
<?php
if(!empty($model->image_thumb_path))
{
echo CHtml::image(Yii::app()->request->baseUrl.'/'.$model->image_thumb_path);
}
else 
{
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().'/../../';
@unlink($image_path.User::model()->findbyPk($model->id)->image_thumb_path);
@unlink($image_path.User::model()->findbyPk($model->id)->image_path);
$model->save();
$this->redirect(array('//user/show','id'=>$model->id));
}

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

#10 User is offline   Anurag Developer 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 07-February 13

Posted 07 February 2013 - 09:09 PM

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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users