Doctrine Tricks — SoftDelete

While pouring over some posts about Doctrine I stumbled upon a very nice solution to the Cascading Delete issue when using the SoftDelete behaviour on an Doctrine Model. Here is what the guys at Elink Media writes;

I have been using Doctrine ORM for a while now. Here I want to discuss 2 tricky issues I have encountered and resolved.

First, I’m paranoid of deleting records from the database. Things like registered users, I would rather have them marked as “DELETED”, but still keep the record in the database, just in case …

With Doctrine, the SoftDelete behaviour is perfect for this purpose. When a model is enabled with the SoftDelete behaviour, an extra field “deleted_at” is automatically added to the corresponding database table by default. So, when you call the delete() function on a model object, the corresponding data row in the database receives a timestamp on the “deleted_at” field. Data having values on the “deleted_at” field will not be returned by any find methods in the future.

It all sounds good, right? Not until you start trying to cascade “delete” on associations. Since we are not really deleting the record, DBMS level delete cascading will simply not work (In Doctrine YAML schema term, the “onDelete: CASCADE” will not work). Now, I’ll illustrate the problem and the solution with a typical blog example.

Click for Solution 🙂

http://blog.elinkmedia.net.au/2010/01/30/doctrine-tricks/.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.