symfony and doctrine: keep the deleted record

by prettyscripts on 2010-05-21 10:39:38

phpsymfony

in most of the projects i work on, when 'deleting' a record, it's preferred to mark it as deleted than actually deleting the record. this makes re-instating a record easier. the common practice is to add a status field to every record.

in doctrine, this can be done with SoftDelete.

when defined, a column 'deleted_at' is automatically created to the table. when deleting the record, this field is automatically updated to the current timestamp.

in schema.yml:

Code:

MyTable:
  actAs:
    SoftDelete:    ~
  columns:
    .....

i wish i had discovered this earlier! it really saves my coding time!

Tags: database, delete, doctrine, symfony

3 comments

Comment by Bitcoder @ 2010-05-26 19:17:38
When you delete like this?

Doctrine_Query::create()->delete()
->from('MyTable t')
->where('t.col_id = 1')
->execute();

This field is set to current time. But what happen to row? How Doctrine knows it is considered as deleted? Finally, How I restablish row to undeleted?

Anyway seems an incredible feature of Doctrine.
Comment by prettyscripts @ 2010-05-27 10:36:18
@Bitcoder, i've never tried deleting records with that with SoftDelete defined. try to test by creating an admin module to see what happens.
as mentioned in the post all it does it just set timestamp to deleted_at. the row still exists. to undelete just set deleted_at to null.
Comment by Karim Samir @ 2010-06-16 21:06:51
****-
How can I retrieve deleted row using Doctrine?

Leave a comment


Your email address will not be revealed on this site.
PoorExcellent
note: all comments are moderated. do not spam and do not advertise. only comments relevant to the post will be published.
(Line breaks become <br />)
(For my next comment on this site)
(Allow users to contact me through a message form -- Your email will not be revealed!)