Symfony and Doctrine: Keep the deleted record

In most of the projects I work on, when ‘deleting’ a record, it’s preferred to mark it as deleted than physically 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:

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

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

3 thoughts on “Symfony and Doctrine: Keep the deleted record

  1. Bitcoder Reply

    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.

  2. prettyscripts Reply

    @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.

  3. Karim Samir Reply

    How can I retrieve deleted row using Doctrine?

Leave a Reply

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