Symfony2:如何使 FOSUserBundle 用户的用户名唯一
我有自己的User
类,它继承FOS\UserBundle\Entity\User
。此外,我还编写了自己的注册例程。现在我遇到的问题是表单无法确保用户名是唯一的。我总是得到:
SQLSTATE[23000]:违反完整性约束:1062 键“UNIQ_2DA1797792FC23A8”的重复条目“myusername”
我尝试添加 @UniqueEntity("email")
注释,如文档中所述1,但没有任何效果。
有人知道可能出了什么问题吗?
I have my own User
Class, which inherits FOS\UserBundle\Entity\User
. Additionally I wrote my own registration routine. Now I have the problem that the form does not make sure that the username is unique. I always get:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'myusername' for key 'UNIQ_2DA1797792FC23A8'
I tried adding the @UniqueEntity("email")
annotation as stated in the documentation1, but without any effect.
Someone knows what might be wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用 fos_user 捆绑包,则可以简单地使用 UniqueEntity 约束: http: //symfony.com/doc/2.0/reference/constraints/UniqueEntity.html。
要实现它,只需确保您的 User 类包含正确的 use 语句,然后包含注释,如下所示(假设您正在使用注释):
If you're using the fos_user bundle, you can simply use the UniqueEntity constraint: http://symfony.com/doc/2.0/reference/constraints/UniqueEntity.html.
To implement it, just make sure your User class constains the proper use statements, and then the annotations, like so (assuming you're using annotations):
FOS 捆绑包中已存在该约束。您可能需要将表单上的
validation_groups
选项设置为array('Registration')
。The constraint exists in the FOS bundle already. You probably need to set the
validation_groups
option on your form toarray('Registration')
.您可以通过用户实体验证在
validation.yml
上尝试此操作:You can try this on the
validation.yml
with your user entity validation: