Thomas Landauer
Thomas Landauer

Reputation: 8355

Make Doctrine's UniqueEntity constraint case-insensitive

Is there a way to configure UniqueEntity to treat foo and Foo as duplicates?

Use case: I have @UniqueEntity("email") on my user entity, since I'm using the email address as username. According to https://stackoverflow.com/a/9808332/1668200 the local part of an email address is case-sensitive in theory, but case-insensitive in practice.

I could certainly convert it to lowercase in setEmail() (as suggested at https://stackoverflow.com/a/31663365/1668200). Drawbacks:

So I'd rather reject the user [email protected]'s registration (if [email protected] is already present), than convert all users' addresses to lowercase.

Upvotes: 2

Views: 1576

Answers (1)

AythaNzt
AythaNzt

Reputation: 1057

Based on this UniqueEntity:

repositoryMethod type: string default: findBy()

The name of the repository method to use for making the query to determine the uniqueness. If it's left blank, the findBy() method will be used. This method should return a countable result.

You could create a function in the repository to find if that email exists applying UPPER() in the sql.

Upvotes: 3

Related Questions