Looking at the Api the beforedelete function is cahined from the del 
function del($id = null, $cascade = true) {
00957         if ($id) {
00958             $this->id = $id;
00959         }
00960 
00961         $id = $this->id;
00962 
00963         if ($this->exists() && $this->beforeDelete()) { 

As you can see on line 958 the passed id is assigned to model->id 

Then you can surely write

function beforeDelete() {
        $photo = $this->Photo->findById($this->id); // NOTE $this->id
instead of $id

        $file_path = substr($photo['Photo']['file_path'], 1); // remove the
leading slash for cake
        // Code from Chris Partridge:
        if($file = new File($file_path)) {
                if(!$file->delete()) {
                        trigger_error('delete error');
                        return false;
                }
        } else {
                return false;
        }
        return true;
}

-----Messaggio originale-----
Da: [email protected] [mailto:[EMAIL PROTECTED] Per conto
di Joshua McFarren
Inviato: sabato 17 marzo 2007 19.49
A: Cake PHP
Oggetto: Re: Class dependence and overriding Methods to do garbage
collection on files


> I'm new to cake so I may not be 100% right, but why not you create a
> beforeDelete() method in your Photo model? Use this delete the 
> associated image file.

This looked promising until i realized that I need the $id to get the file
path of the photo and delete it. I would need to put something like this in
my Model:

function beforeDelete($id) {
        $photo = $this->Photo->findById($id);
        $file_path = substr($photo['Photo']['file_path'], 1); // remove the
leading slash for cake
        // Code from Chris Partridge:
        if($file = new File($file_path)) {
                if(!$file->delete()) {
                        trigger_error('delete error');
                        return false;
                }
        } else {
                return false;
        }
        return true;
}

Unfortunately, looking at the API the beforeDelete method takes no arguments
when its called by del:

http://api.cakephp.org/model__php4_8php-source.html#l00956

So how would my beforeDelete method know what to do?

Is this a dead end or am I wrong?




--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to