EntityType and soft-deletion: the lib you should not use
(php, symfony) 1 min read by Bartosz Pachołek
A Symfony form type that keeps edit forms working when a related entity was soft-deleted, and why needing it is a warning sign.
Originally published on LinkedIn in July 2024.
With the softdeleteable filter from doctrine/extensions and Symfony's EntityType, an edit form breaks as soon as a related entity has been soft-deleted: instead of the form you get "Entity of type ... for IDs id(1) was not found".
SoftDeleteAwareEntityType is the missing link. It renders the form anyway, falls back to the placeholder, and makes the user pick a new relation.
composer require idct/symfony-form-soft-delete-aware-entity-type
Register it as a service and use it where you used EntityType.
Why you should not need it
If you need this, the architecture most likely has a problem. When you allow deletion or soft deletion, every related entity should first be updated with null or a new relation. The form type is meant for transitions: systems that must add soft deletion to entities used in relations quickly, when there is no time to rework the actual processes.
It works with Symfony 6 and 7 and with Sonata Admin 3+, under the MIT license.