08-06-2023, 04:23 PM
I have the following entity class in my application:
<?php
namespace ...;
// use ...
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;;
// ...
/**
* @ORM\Table(name="sc_user")
* @ORM\Entity(repositoryClass="...\UserRepository")
* @ORM\HasLifecycleCallbacks()
* @UniqueEntity(fields={"email", "username"})
*/
class User implements UserInterface, \Serializable, EquatableInterface
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $email
*
* @ORM\Column(name="email", type="string", length=255, unique=true)
*
* @Assert\NotBlank(groups={"registration"})
* @Assert\Email(groups={"registration"})
*/
private $email;
/**
* @var string $username
*
* @ORM\Column(name="username", type="string", length=32, unique=true)
*
* @Assert\NotBlank(groups={"registration"})
*/
private $username;
// ...
}
The @UniqueEntity constraint is being ignored. I tried different flavors including:
@UniqueEntity(fields={"email", "username"})
and
@UniqueEntity(fields={"email"})
@UniqueEntity(fields={"username"})
and, as per the Symfony2 documentation here: [
@UniqueEntity("email")
@UniqueEntity("username")
Nothing I do, works. Instead of getting a form validation error as expected, I am getting the following exception:
> SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry
> '[email protected]' for key 'UNIQ_D8183973E7927C74'
This is just wrong! Does anyone know how to fix this issue?
[1]:
<?php
namespace ...;
// use ...
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;;
// ...
/**
* @ORM\Table(name="sc_user")
* @ORM\Entity(repositoryClass="...\UserRepository")
* @ORM\HasLifecycleCallbacks()
* @UniqueEntity(fields={"email", "username"})
*/
class User implements UserInterface, \Serializable, EquatableInterface
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $email
*
* @ORM\Column(name="email", type="string", length=255, unique=true)
*
* @Assert\NotBlank(groups={"registration"})
* @Assert\Email(groups={"registration"})
*/
private $email;
/**
* @var string $username
*
* @ORM\Column(name="username", type="string", length=32, unique=true)
*
* @Assert\NotBlank(groups={"registration"})
*/
private $username;
// ...
}
The @UniqueEntity constraint is being ignored. I tried different flavors including:
@UniqueEntity(fields={"email", "username"})
and
@UniqueEntity(fields={"email"})
@UniqueEntity(fields={"username"})
and, as per the Symfony2 documentation here: [
[To see links please register here]
][1]@UniqueEntity("email")
@UniqueEntity("username")
Nothing I do, works. Instead of getting a form validation error as expected, I am getting the following exception:
> SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry
> '[email protected]' for key 'UNIQ_D8183973E7927C74'
This is just wrong! Does anyone know how to fix this issue?
[1]:
[To see links please register here]