Search code examples
symfonysymfony2-easyadmin

How to check if there is a record duplicate/doublon at EasyAdmin prePersist


In EasyAdminBundle, do you know a way to check if there is an entity duplicate before letting EasyAdminBundle perform the "persist & flush".

Trial : I tried to override the AdminController and added prePersistEntity() containing my "duplicate check" logic, but I failed at setting a redirection when detecting an upcoming doublon. The prePersist event logic continue to execute the "newAction()" code and finally "persist & flush", ignoring my redirection.

How to manage properly duplicate check and setting a redirection within the EasyAdmin overriding logic?

Thanks.


Solution

  • I think your problem is related to a missing check for duplicate on your entity. To avoid duplicate you have to use the @UniqueEntity annotation.

    There is an official documentation on how to implement it : https://symfony.com/doc/current/reference/constraints/UniqueEntity.html