views:

111

answers:

2

I want to link a physical file with a row in a table. My intention is to use database habilities to delete the files that are referenced in the table. For example:

$o = Doctrine::getTable('Document')->find(12); $o->delete();

This code delete the row in the table, i want to delete an hipotetical file referenced in $o->file_location. I'm trying it with Events (preDelete, postDelete, preUpdate, postUpdate) but i can't make it works.

A: 

can't you just unlink the file?

like this:

$o = Doctrine::getTable('Document')->find(12);
if(unlink($o->file_location))
{
    $o->delete();
}
RJD22
+2  A: 

In your Document model i would add something like this:

class Document extends BaseDocument
{
    ...

    public function preDelete($event)
    {
        unlink($this->file_location);
    }

    ...
}

Also, Doctrine has the Doctrine_Search_Files class which indexes (directories of) files for searching. Maybe you can get some inspiration there?

Karsten
First, thanks a lot for answer my question, but i think your code doesn't works, you need to get the invoker with $event->getInvoker(). And I preffer use Record_Listener instead Record Hooks.
elManolo