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:

    SoftDelete:    ~

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?

    ->from(‘MyTable t’)
    ->where(‘t.col_id = 1’)

    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 *