我正在尝试在我的用户实体上使用唯一的实体约束但不知何故它不起作用。我仍然收到重复密钥的错误消息。
这是我的用户实体:
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Table(name="Users")
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
* @UniqueEntity(
* fields={"email", "username"}
* )
*/
class User implements AdvancedUserInterface, \Serializable
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @ORM\Column(type="string", length=25, unique=true)
*/
public $username;
/**
* @ORM\Column(type="string", length=64)
*/
public $password;
/**
* @ORM\Column(type="string", length=128, unique=true)
* @Assert\Email()
*/
public $email;
/**
* @ORM\Column(name="is_active", type="boolean")
*/
public $isActive;
/**
* @ORM\Column(name="roles", type="array")
*/
public $roles = [];
/**
* @ORM\OneToOne(targetEntity="App\Entity\UserDetails", mappedBy="user", cascade={"persist", "remove"})
*/
private $userDetails;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Assignment", mappedBy="user")
*/
private $assignments;
public function __construct()
{
$this->isActive = true;
$this->assignments = new ArrayCollection();
// may not be needed, see section on salt below
// $this->salt = md5(uniqid('', true));
}
/**
* @param mixed $isActive
*/
public function setIsActive($isActive)
{
$this->isActive = $isActive;
}
public function getEmail()
{
return $this->email;
}
public function setEmail($email)
{
$this->email = $email;
}
public function getUsername()
{
return $this->username;
}
public function setUsername($username)
{
$this->username = $username;
}
public function getPassword()
{
return $this->password;
}
public function setPassword($password)
{
$this->password = $password;
}
public function getSalt()
{
// The bcrypt and argon2i algorithms don't require a separate salt.
// You *may* need a real salt if you choose a different encoder.
return null;
}
public function getRoles()
{
$roles = $this->roles;
if (!in_array('ROLE_USER', $roles)) {
$roles[] = 'ROLE_USER';
}
return $roles;
}
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
public function setRoles(array $roles)
{
$this->roles = $roles;
}
public function eraseCredentials()
{
}
/** @see \Serializable::serialize() */
public function serialize()
{
return serialize(array(
$this->id,
$this->username,
$this->password,
$this->isActive
// see section on salt below
// $this->salt,
));
}
/** @see \Serializable::unserialize() */
public function unserialize($serialized)
{
list (
$this->id,
$this->username,
$this->password,
$this->isActive
// see section on salt below
// $this->salt
) = unserialize($serialized, ['allowed_classes' => false]);
}
public function isAccountNonExpired()
{
return true;
}
public function isAccountNonLocked()
{
return true;
}
public function isCredentialsNonExpired()
{
return true;
}
public function isEnabled()
{
return $this->isActive;
}
public function getUserDetails(): ?UserDetails
{
return $this->userDetails;
}
public function setUserDetails(UserDetails $userDetails): self
{
$this->userDetails = $userDetails;
// set the owning side of the relation if necessary
if ($this !== $userDetails->getUser()) {
$userDetails->setUser($this);
}
return $this;
}
/**
* @return Collection|Assignment[]
*/
public function getAssignments(): Collection
{
return $this->assignments;
}
public function addAssignment(Assignment $assignment): self
{
if (!$this->assignments->contains($assignment)) {
$this->assignments[] = $assignment;
$assignment->addUser($this);
}
return $this;
}
public function removeAssignment(Assignment $assignment): self
{
if ($this->assignments->contains($assignment)) {
$this->assignments->removeElement($assignment);
$assignment->removeUser($this);
}
return $this;
}
}
我在提交表单时收到以下错误消息:
使用params [“sanderbakker”,“$ 2y $ 13 $ evlC1NcG9BIdgeBRuBqCKu4uqt4Edsp24SEUIiYF3vg6rp0tEhHdm”执行'INSERT INTO Users(用户名,密码,电子邮件,is_active,角色)VALUES(?,?,?,?,?)'时发生异常,“sander@crmfabriek.nl”,1,“a:1:{i:0; s:10:\”ROLE_ADMIN \“;}”]:
SQLSTATE [23000]:完整性约束违规:1062重复条目“UNIQ_D5428AEDF85E0677”的重复条目“sanderbakker”
如果我只使用一个属性作为约束,则不会出现此错误。例如,仅用户名属性或电子邮件属性
答案 0 :(得分:0)
这正是“UniqueEntity”约束的工作原理。 它添加了对用户名和电子邮件组合的控制,如果您尝试插入包含已存在于表中的电子邮件和用户名的记录,则orm会引发异常。 您可以在刷新之前检查组合是否已经存在,或者,如果您愿意,可以处理您可以将刷新操作插入到这样的try / catch块中
try {
$em->flush();
} catch (Doctrine\DBAL\Exception\UniqueConstraintViolationException $e) {
// handle it here maybe returning an error to the UI
}