Yii Framework Forum: Any onBeforeDelete example ? - Yii Framework Forum

Jump to content

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

Any onBeforeDelete example ? Cevent handling Rate Topic: -----

#1 User is offline   saegeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 244
  • Joined: 09-December 09
  • Location:Montpellier - France

Posted 10 April 2012 - 04:07 AM

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 ?
And [for] their saying, "Indeed, we have killed the Messiah, Jesus, the son of Mary, the messenger of God ." And they did not kill him, nor did they crucify him; but [another] was made to resemble him to them. And indeed, those who differ over it are in doubt about it. They have no knowledge of it except the following of assumption. And they did not kill him, for certain.Rather, God raised him to Himself.
0

#2 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,993
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 10 April 2012 - 08:34 AM

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

#3 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,993
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 10 April 2012 - 08:37 AM

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

#4 User is offline   saegeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 244
  • Joined: 09-December 09
  • Location:Montpellier - France

Posted 10 April 2012 - 02:31 PM

View PostMike, on 10 April 2012 - 08:34 AM, said:

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


Ok and then ?
any example ?

where i could put my unlink() functions ?
And [for] their saying, "Indeed, we have killed the Messiah, Jesus, the son of Mary, the messenger of God ." And they did not kill him, nor did they crucify him; but [another] was made to resemble him to them. And indeed, those who differ over it are in doubt about it. They have no knowledge of it except the following of assumption. And they did not kill him, for certain.Rather, God raised him to Himself.
0

#5 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,993
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 10 April 2012 - 02:57 PM

... 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);
}

0

#6 User is offline   saegeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 244
  • Joined: 09-December 09
  • Location:Montpellier - France

Posted 11 April 2012 - 12:55 AM

View PostMike, on 10 April 2012 - 02:57 PM, said:

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

}

And [for] their saying, "Indeed, we have killed the Messiah, Jesus, the son of Mary, the messenger of God ." And they did not kill him, nor did they crucify him; but [another] was made to resemble him to them. And indeed, those who differ over it are in doubt about it. They have no knowledge of it except the following of assumption. And they did not kill him, for certain.Rather, God raised him to Himself.
0

#7 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,993
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 11 April 2012 - 01:35 AM

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

#8 User is offline   saegeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 244
  • Joined: 09-December 09
  • Location:Montpellier - France

Posted 12 April 2012 - 01:57 AM

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();

And [for] their saying, "Indeed, we have killed the Messiah, Jesus, the son of Mary, the messenger of God ." And they did not kill him, nor did they crucify him; but [another] was made to resemble him to them. And indeed, those who differ over it are in doubt about it. They have no knowledge of it except the following of assumption. And they did not kill him, for certain.Rather, God raised him to Himself.
0

#9 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,993
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 12 April 2012 - 02:02 AM

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

#10 User is offline   saegeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 244
  • Joined: 09-December 09
  • Location:Montpellier - France

Posted 12 April 2012 - 10:02 AM

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 ?
And [for] their saying, "Indeed, we have killed the Messiah, Jesus, the son of Mary, the messenger of God ." And they did not kill him, nor did they crucify him; but [another] was made to resemble him to them. And indeed, those who differ over it are in doubt about it. They have no knowledge of it except the following of assumption. And they did not kill him, for certain.Rather, God raised him to Himself.
0

#11 User is offline   saegeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 244
  • Joined: 09-December 09
  • Location:Montpellier - France

Posted 12 April 2012 - 10:12 AM

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);
        }

And [for] their saying, "Indeed, we have killed the Messiah, Jesus, the son of Mary, the messenger of God ." And they did not kill him, nor did they crucify him; but [another] was made to resemble him to them. And indeed, those who differ over it are in doubt about it. They have no knowledge of it except the following of assumption. And they did not kill him, for certain.Rather, God raised him to Himself.
0

#12 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 1,531
  • Joined: 16-February 11
  • Location:Japan

Posted 12 April 2012 - 12:09 PM

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.

2) 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'.
0

#13 User is offline   saegeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 244
  • Joined: 09-December 09
  • Location:Montpellier - France

Posted 12 April 2012 - 12:50 PM

@softark: Many thanks ! It works as expected !

Your example should be displayed in the class reference.
And [for] their saying, "Indeed, we have killed the Messiah, Jesus, the son of Mary, the messenger of God ." And they did not kill him, nor did they crucify him; but [another] was made to resemble him to them. And indeed, those who differ over it are in doubt about it. They have no knowledge of it except the following of assumption. And they did not kill him, for certain.Rather, God raised him to Himself.
0

#14 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,993
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 13 April 2012 - 02:21 AM

Posted Image
0

#15 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 1,531
  • Joined: 16-February 11
  • Location:Japan

Posted 13 April 2012 - 02:37 AM

:(
0

Share this topic:


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

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