How Delete Article Images?

Hi,

How to delete the article images when is confirmed the article deletion?

Any ideas ?

Thanks

You could try to use unlink.

try this




$folder = YiiBase::getPathOfAlias('webroot') . '/upload/event/' . $_POST['imagename'];

            if (file_exists($folder) && $_POST['imagename'] != "") {

                @unlink($folder);

                @unlink(YiiBase::getPathOfAlias('webroot') . '/upload/event/thumb81/' . $_POST['imagename']);

               

            }

I hope it’s some help.

Thx …

But main is more simple …




                         $Article = $this->loadModel($id);

                        if($this->loadModel($id)->delete()){

                             @unlink(Yii::getPathOfAlias('webroot').'/images/article/'.$Article->main_image);

                        }



But the problem is bigger :(

The table include col name "cid" related to category, my code delete all the articles & image which under one "cid" ??!

How to debug this?

Thx

I’m not sure I understand you. You want to delete all of the articles and images with a particular category id? Your code looks like it should work just fine to delete one article and its image.

If you do want to delete articles and images that belong to a specific category, you could try the following.




$articles = Article::model()->findAllByAttributes(array('cid'=>$someCategoryId));

foreach($articles as $article){

 if($article->delete()){

   @unlink(Yii::getPathOfAlias('webroot').'/images/article/'.$article->main_image);

 }

}



You could also try a custom query instead of creating all those model objects, get the main_image of each article, and then delete both the images and the articles.

What I mean my code is deleting all articles which is have the cid as the wanted article.

Hi marvix,

It sounds like you have some error in your handling of the related objects.

Don’t you try to remove the category in your Article::afterDelete()?

Hello,

I do want to remove the category, the delete funciton is delete it all the records with have same category ID when am deleting one record.

The function afterDelete does not exist in the model …

The form code:




echo CHtml::ajaxLink('delete',

                Yii::app()->createUrl('/Article/Delete/'.$model->id),array(

    												                            'type'=>'post',

    												                            'data' => array('id' =>$model->id,'type'=>'delete'),

    												                            'success' => "function() {

    												                            							window.location.replace('".$AdminPage."')

    												                            							}",

    												                            	),

    												                            	array(	'confirm'=>'Are you sure?', 

    												                            			"class"=>'btn btn-danger btn-small pull-left moa'

    												                            	)




The delete function in the controller:




public function actionDelete($id)

        {

         

                if(Yii::app()->request->isPostRequest)

                {


                         $Article = $this->loadModel($id);

                        if($this->loadModel($id)->delete()){

                             @unlink(Yii::getPathOfAlias('webroot').'/images/article/'.$Article->main_image);

                        }

             // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser

                        if(!isset($_GET['ajax']))

                                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));

                }

                else

                        throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');

                

        }



2 days spent on this error …

Also tired with this code:




                         $Article = $this->loadModel($id);

                         if ( $Article->deleteByPk($id) ) {

                             @unlink(Yii::getPathOfAlias('webroot').'/images/article/'.$Article->main_image);

                             @unlink(Yii::getPathOfAlias('webroot').'/images/article/'.$Article->slider_image);                         

                         }




and its the same …

strange … deleting one record from db with sql query :


DELETE FROM `article` WHERE `article`.`id`='7802'

result deleting all records which have same cid !!

could this because of relations & foreign key ?

Probably you have set up a wrong FK constraint, or a bad trigger to delete a record in category table.

I found it yesterday … it was cascade on delete on child level, delete it one record will delete all records with same CID as cascaded …

Thanks for being helpful ;)