Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony UniqueEntity vs UniqueConstraint vs unique=true

Can anyone explain what's the conceptual difference between @UniqueEntity validator, @UniqueConstraint table annotation and unique=true option of @Column annotation.

I understand that @UniqueConstraint adds UNIQUE index on database level and @UniqueEntity validates on ORM level. So what option shall I use, or do I use all of them?

like image 508
genesst Avatar asked Sep 25 '17 22:09

genesst


People also ask

What is the required option for a uniqueentity constraint?

This required option is the field (or list of fields) on which this entity should be unique. For example, if you specified both the email and name field in a single UniqueEntity constraint, then it would enforce that the combination value is unique (e.g. two users could have the same email, as long as they don't have the same name also).

What is the unique constraint in Symfony?

This is commonly used, for example, to prevent a new user to register using an email address that already exists in the system. If you want to validate that all the elements of the collection are unique use the Unique constraint. In order to use this constraint, you should have installed the symfony/doctrine-bridge with Composer.

What is the difference between @uniqueconstraint and unique=true in doctrine?

@UniqueConstraint and unique=true are part of Doctrine and do similar thing. When you set unique=true on a particular column, then Doctrine will create a unique key on this column physically in database. @UniqueConstraint can be used to create a unique key in database on multiple columns (complex unique key).

How do I require two fields to be individually unique?

If you need to require two fields to be individually unique (e.g. a unique email and a unique username ), you use two UniqueEntity entries, each with a single field. It defines the validation group or groups of this constraint.


2 Answers

@UniqueConstraint and unique=true are part of Doctrine and do similar thing.

When you set unique=true on a particular column, then Doctrine will create a unique key on this column physically in database.

@UniqueConstraint can be used to create a unique key in database on multiple columns (complex unique key). But if you pass a single column, then the result will be exactly the same as using unique=true on that field.

@UniqueEntity on the other hand is not a part of Doctrine, but it's a part of Symfony framework. While options above are used by Doctrine to generate proper schema, this one is just a validator used usually by Symfony Form Component at time of submitting the form.

So to answer your final question - yes, you usually should use both @UniqueEntity and one of @UniqueConstraint or unique=true.

like image 55
Jakub Matczak Avatar answered Sep 20 '22 17:09

Jakub Matczak


As stated in documentation, @UniqueConstraint annotation is used for creation of unique constraint on multiple columns, when unique=true is used for unique constraint on one column.

UniqueEntityValidator exists to show friendly error message and unique database constraint's purpose is to make sure you don't store duplicate data.

So the answer to your question is like this - you should use both database constraint and @UniqueValidator.

like image 29
svgrafov Avatar answered Sep 21 '22 17:09

svgrafov