Dec 18
Digg
Stumbleupon
Technorati
Delicious

postDelete in doctrine

This post is not just about Doctrine’s postDelete method. So as you know Doctrine supports a few useful methods which extends insert / delete functionality (postDelete, preDelete, postInsert, preInsert, etc).

The key thing – there is documented the way to use them inside Doctrine listeners (see e.g. here http://www.symfony-project.org/more-with-symfony/1_4/en/08-Advanced-Doctrine-Usage) but it’s not obvious how to use it inside Doctrine model classes.

So first thing you may want to try to add in ClassName.class.php:

public function postDelete()
{
}

But it won’t really work and warn you:

Strict Standards: Declaration of ClassName::postDelete() should be compatible with that of Doctrine_Record::postDelete() in ClassName.class.php on line X

If you’d try to add this method as (like mentioned in article above):

public function postDelete(Doctrine_Event $event)

it would not work as well. The way to get it working is as follows:

public function postDelete($values)
{
}

The one of possible applications for postDelete method – delete extra objects related with Doctrine record, lets consider here situation when we need to delete related folder with files inside of it.
In style of symfony (so using symfony methods to delete files and folders) it would look like this:

  public function postDelete($values)
  {
    $dir = $this->getDir();
    sfToolkit::clearDirectory($dir);
    $sf = new sfFilesystem();
    $sf->remove($dir);
  }

where getDir() method which we omitted returns absolute path to dir.
It’s worth to mention we’ve used here 2 great symfony classes sfToolkit and sfFilesystem:

http://www.symfony-project.org/api/1_4/sfToolkit
http://www.symfony-project.org/api/1_4/sfFilesystem

As we’ve mentioned in beginning it’s not only about postDelete – the same approach may be used for preDelete, etc


Author: symfonian

No Comments

No comments yet.

Comments RSS TrackBack Identifier URI

Leave a comment