How can I verify that the username and the mail aren't already present in my database before the insertion.
I tried with the isValid()
method but it doesn't work.
My Entity:
<?php
namespace Fiducial\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
/**
* User
*
* @ORM\Table(name="utilisateurPortail")
* @ORM\Entity(repositoryClass="Fiducial\UserBundle\Repository\UtilisateurPortailRepository")
*/
class UtilisateurPortail extends BaseUser
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
Inside my controller:
if ($form->isValid()) {
if($userManager->findUserByUsername($user->getUsername()) != null) {
$message = 'Nom d\'utilisateur déjà utilisé';
} elseif($userManager->findUserByEmail($user->getEmail()) != null) {
$message = 'Email déjà utilisé';
} else {
$userManager->updateUser($user);
$message = 'Utilisateur ajouté !';
}
}
I get this error/exception when I try to save a user with a username that already exists to database:
An exception occurred while executing 'INSERT INTO utilisateurPortail (username, username_canonical, email, email_canonical, enabled, salt, password, last_login, locked, expired, expires_at, confirmation_token, password_requested_at, roles, credentials_expired, credentials_expire_at, id, raisonsociale, nom, prenom, fonction, envoiSMS, telephone, premiere_connexion) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params ["dd", "dd", "[email protected]", "[email protected]", 1, "fhaqicni2sgko4o8s4804oo8g48wcgw", "tdsZpiC1Tzbh5mAVf23uyp9hnXDv39eEXzBh4/P3/DECu5z3QbqGHJjSSy6ccpir6T35rS5r043WSacBqazJNA==", null, 0, 0, null, null, null, "a:1:{i:0;s:16:\"ROLE_SUPER_ADMIN\";}", 0, null, 8, "sdfsdq", "aaa", "aaa", "dsfvsdf", 1, "3333333333", 1]:
SQLSTATE[23505]: Unique violation: 7 ERREUR: la valeur d'une clé dupliquée rompt la contrainte unique « uniq_1e48ea092fc23a8 » DETAIL: La clé « (username_canonical)=(dd) » existe déjà.