Any onBeforeDelete example ?

Hi,

I just added this into my model but it doesn’t work :




protected function onBeforeDelete($event) {

parent::onBeforeDelete($event);

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

unlink($this->rootPath.$this->thumb_url);

unlink($this->rootPath.$this->image_url);

}



I think i need to initiaize a new event from the controller.

My delete action in controller is the default delete action :




    public function actionDelete($id) {

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

        {

            // we only allow deletion via POST request

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

            

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

    }



I get a blank page because of $event was not initialized.

Should i have to initialize it into the controller or into the model?

Any example please ?

You should better override "beforeDelete" not "onBeforeDelete".

… oh, and you probably want to use "afterDelete" instead. Because you want your files only deleted if the DB operation was successful. Otherwhise you could end up with stale DB rows in the error case.

Ok and then ?

any example ?

where i could put my unlink() functions ?

… not sure where your problem is but let me copy & paste your code:




<?php

 protected function beforeDelete() {

    parent::beforeDelete();

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

    unlink($this->rootPath.$this->thumb_url);

    unlink($this->rootPath.$this->image_url);

}

You already said that. I’m searching for a working example where $event is initialized.

Because I don’t know how i can initialize $event so i get i blank page (syntax error).

This portion of code is an auto-suggestion of Netbeans IDE :




protected function onBeforeDelete($event) {

parent::onBeforeDelete($event);


}



Sorry, but I don’t understand your problem. You got a working example. I know, that this code works, because i use similar code in my applications. I don’t know, why you still want to override onBeforeDelete(). Just because your IDE suggests some code does not mean, that this is the right code to use. So did you try the above? Does it work? If not, we should rather find out, what in your code prevents it from working.

Finally I found why i get a blank page.

Because the function onBeforeDelete() must be declared as public, not as protected !

And the example i was searching for is the following :




$this->model->onAfterDelete(new CEvent($this));

$this->model->delete();



You should not do it this way. You now manually trigger the onAfterDelete event - again! ActiveRecord already does this for you. If you want code to get executed after delete, then you should either override afterDelete() or attach a handler to the onAfterDelete event. I still can’t understand why you’re so obsessed with using the onAfterDelete() method… :)

Ok, so the only code I need is @ model (override method) :




        public function onAfterDelete($event) {

            parent::afterDelete($event);

            if () {

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

                unlink($this->rootPath.$this->thumb_url);

                unlink($this->rootPath.$this->image_url);

            }

        }



in the if() i need to say "if the deletion was successful".

Is there a CActiverecord property setted to true when the deletion was successful ?

Finally the ONLY code that works is :

@controller :




$this->loadModel($id);

if ($this->model->delete()!=false)

$this->model->onAfterDelete(new CEvent($this));



@model:




        public function onAfterDelete($event) {

            parent::afterDelete($event);

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

                unlink($this->rootPath.$this->thumb_url);

                unlink($this->rootPath.$this->image_url);

        }



As Mike has already stated, onAfterDelete() is not meant to be used like that.

Please forget about it, it’s a kind of reserved-ONLY-for-OOP-geek matter.

We, ordinary programmers, can do without it, overriding afterDelete() for our boring simple tasks. :)




// @controller :

$this->loadModel($id);

$this->model->delete();






// @model:

protected function afterDelete() {

    parent::afterDelete();

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

    unlink($this->rootPath.$this->thumb_url);

    unlink($this->rootPath.$this->image_url);

}



  1. afterDelete() is automatically called when you have called delete() and when the deletion has completed without errors.

You don’t have to call it manually.

  1. afterDelete() is a protected function. It’s meant to be called from inside the class, not from the outside.

In general, protected functions are for internal use. We can not and should not call it from outside the class. Don’t alter it to ‘public’.

@softark: Many thanks ! It works as expected !

Your example should be displayed in the class reference.

:(