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?
… 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.
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.
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 ?