I'm using symfony 4.2 and ReactJS. I have a form with a mail. This mail should be unique. So I have a UniqueEntity included in Entity.
The issue is the following when I try to create an account with the form, it throws me an error 500 : "An error occurred","hydra:description":"An exception occurred while executing \u0027INSERT INTO app_users (id, username, email, is_active, firstname, lastname, api_link_key, facebook_id, facebook_picture_url) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)\u0027 with params XXXXXX : Unique violation: 7 ERROR: duplicate key value violates unique constraint \u0022uniq_c2502824f85e0677\u0022\nDETAIL: Key (username)=(XXXXXX) already exists."
So I don't have a message error in the form maybe because of this error 500 ? Or maybe should I set The message error somewhere ?
In my entity when I set the mail field, I set the username field too with the same value. The UniqueEntity forbid to have the same value in two fields in the same row ?
My entity :
* @ORM\Table(name="app_users")
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
* @UniqueEntity("email")
*/
class User implements UserInterface, \Serializable
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=254, unique=true)
* @Encrypted
*/
private $username;
/**
* @ORM\Column(type="string", length=255, unique=true)
* @Assert\Email()
* @Assert\NotBlank()
* @Encrypted
*/
private $email;
[...]
public function getEmail()
{
return $this->email;
}
public function setEmail($email): void
{
$this->email = $email;
$this->username = $email;
}
}
Thanks for your help
emailcolumn, but about the username which you also declared as unique. So what you probably want to do is add an additional constraint that also enforces uniqueness for the username:@UniqueEntity("username")