afterDelete .. how to call it?

Hi,

I need to call the a function after deleting a record

The model




protected function afterDelete()

 {

     parent::afterDelete();        

    Tags:tagDelete($id);


    }



controller


    public function actionDelete($id)

    {

        $this->loadModel($id)->delete();

        Tags:tagDelete($id);

        // 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'));

    }



The tag model


public static function tagDelete($entity_id){


        //find the tag

        $tags = Yii::app()->db->createCommand()

            ->select('*')

            ->from('tagged')

            ->where("entity_id = $entity_id")

            ->queryRow();

        var_dump($tags);

        // Get the tag data

        $tagData = Yii::app()->db->createCommand()

            ->select('count')

            ->from('tags')

            ->where('tag="'.$tags["tag"].'"')->queryRow();

        var_dump($tagData);

        // update the tag counter

        Yii::app()->db->createCommand()

            ->update('tags', array('count'=>$tagData['count']-1), 'id=:id',array('id'=>$tags["id"]) );

        //Delete the tag

        Yii::app()->db->createCommand()

            ->delete('tagged','entity_id=:id', array('id'=>$entity_id) );

    } 

The tagDelete() works with no problem, but it been called after the post is deleted.

What to do?

THX

Hi marvix

What is the problem ?

If I inderstood well, you have a post record related with tags.

After of delete of Post you have to delete the related tags EXCEPT if you have foreign key to the Post Id.

In this case you have to run tagDelete first, so you have to call it on beforeDelete of the post model

If the problem is not the above please give us more details about it

konapaz

The problem might be you are referencing your static function wrong.

Try adding double colon instead both in the references in model and controller:


Tags::tagDelete($id);