UniqueEntity
Warning: You are browsing the documentation for Symfony 2.x, which is no longer maintained.
Read the updated version of this page for Symfony 7.2 (the current stable version).
Validates that a particular field (or fields) in a Doctrine entity is (are) unique. This is commonly used, for example, to prevent a new user to register using an email address that already exists in the system.
Applies to | class |
Options | |
Class | UniqueEntity |
Validator | UniqueEntityValidator |
Basic Usage
Suppose you have an AppBundle bundle with a User
entity that has
an email
field. You can use the UniqueEntity
constraint to guarantee
that the email
field remains unique between all of the constraints in
your user table:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// src/AppBundle/Entity/Author.php
namespace AppBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
// DON'T forget this use statement!!!
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Entity
* @UniqueEntity("email")
*/
class Author
{
/**
* @var string $email
*
* @ORM\Column(name="email", type="string", length=255, unique=true)
* @Assert\Email
*/
protected $email;
// ...
}
Caution
This constraint doesn't provide any protection against race conditions. They may occur when another entity is persisted by an external process after this validation has passed and before this entity is actually persisted in the database.
Options
fields
type: array
| string
[default option]
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).
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.
message
type: string
default: This value is already used.
The message that's displayed when this constraint fails.
You can use the following parameters in this message:
Parameter | Description |
---|---|
{{ value }} |
The current (invalid) value |
em
type: string
The name of the entity manager to use for making the query to determine the uniqueness. If it's left blank, the correct entity manager will be determined for this class. For that reason, this option should probably not need to be used.
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.
errorPath
type: string
default: The name of the first field in fields
If the entity violates the constraint the error message is bound to the first field in fields. If there is more than one field, you may want to map the error message to another field.
Consider this example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
// src/AppBundle/Entity/Service.php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Entity
* @UniqueEntity(
* fields={"host", "port"},
* errorPath="port",
* message="This port is already in use on that host."
* )
*/
class Service
{
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Host")
*/
public $host;
/**
* @ORM\Column(type="integer")
*/
public $port;
}
Now, the message would be bound to the port
field with this configuration.
ignoreNull
type: boolean
default: true
If this option is set to true
, then the constraint will allow multiple
entities to have a null
value for a field without failing validation.
If set to false
, only one null
value is allowed - if a second entity
also has a null
value, validation would fail.
payload
type: mixed
default: null
This option can be used to attach arbitrary domain-specific data to a constraint. The configured payload is not used by the Validator component, but its processing is completely up to you.
For example, you may want to use several error levels to present failed constraints differently in the front-end depending on the severity of the error.