Search code examples
symfonydoctrineentityunique

UniqueEntity Symfony 3 incorrect fields


I have an existing code (which perfectly works)

* @UniqueEntity( * fields={"email", "transport"}, * message="Error message" * )

But now I need to update this code. I want to a unique entity by 3 fields. For example - fields={"user", "email", "transport"}.

My question: why the second code (with 3 fields) doesn't work at all?


Solution

  • As described in Symfony Docs, when you add an Array of properties to the UniqueEntity, then it would enforce that the combination value is unique.

    So, in your case, can exists more than one record with same email and transport if the user field is different.

    If you need to require two fields to be individually unique (e.g. a unique email and a unique user), you use two UniqueEntity entries, each with a single field.