Erich
Erich

Reputation: 545

How to set a deleted property to true rather than removing a related doctrine entity in Symfony

I'm building an app that allows a user to create reports for advertisers. The entities are set up so that there is a relation between the Report object and the Advertiser object - so that the advertiser has a getReports() method to get them.

I would like to change the app so that instead of actually deleting entities, that it simply changes a "deleted" property to true. That part is no problem, but I'm unsure how to make it so that the getReports() on the Advertiser entity only returns reports for the advertiser that have a deleted property of false.

Please let me know if you have any suggestions how that should be done in accordance with Symfony best practices.

Upvotes: 0

Views: 784

Answers (1)

domagoj
domagoj

Reputation: 946

You should look into Gedmo Doctrine Extensions. http://atlantic18.github.io/DoctrineExtensions/

Specifically for your case: http://atlantic18.github.io/DoctrineExtensions/doc/softdeleteable.html

TLDR; they allow you to configure behavior of your entities in a way you desire, so for example when you "delete" an entity, Gedmo's listeners would set it's deleted value to a current datetime. Now you'd still have that record in your database but with not null value of deleted column marking it 'soft deleted', so when querying, it wouldn't be returned (because Doctrine knows how to query these stuff and would add a condition i.e.: ... where deleted ...) unless you explicitly say you want to see those soft deleted records.

Upvotes: 2

Related Questions