Symfony UniqueEntity Exception



好吧,我知道这个问题已经被问了一百万次了,但现在开始了。

我读过这个,这个,这个以及文档,但仍然不明白我的问题是什么。每当我尝试添加一个用户名/电子邮件重复的用户时,我都会收到一个异常,而不是预期的表单验证错误。我有一个SystemUser实体,它通过联接继承映射链接到各种其他用户类型。在SystemUser类中,我有这个。。。

/**
* SystemUser
*
* @ORMTable(name="systemuser")
* @ORMEntity(repositoryClass="MyBundleMainBundleEntityRepositorySystemUserRepository")
* @ORMInheritanceType("JOINED")
* @ORMDiscriminatorColumn(name="discr", type="integer")
* @ORMDiscriminatorMap({"0" = "SystemUser", "1" = "SchoolAdmin", "2" = "Teacher", "3" = "Student", "4" = "Guardian", "5" = "SystemAdmin"})
* @ORMHasLifecycleCallbacks()
* @UniqueEntity(fields={"email"}, message="This email address is already used in the system", groups={"registration"})
* @UniqueEntity(fields={"username"}, message="This cellphone number is already used in the system", groups={"registration"})
*/

在字段本身(电子邮件、用户名)上,我还有unique=true。我试过没有它,同样的例外。我也尝试过不使用groups={"registration"}标志,但我不确定是否必须在某个地方指定和声明"registration"组(我确实检查了文档,但找不到任何内容)。事实上,我不知道验证组在这个例子中到底有什么好处,但我还是尝试了一下。

失败了,我开始尝试验证。yml…

MyBundleMainBundleEntitySystemUser:
constraints:
- SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity: email
- SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity: username
properties:
email:
- Email: { groups: [registration] }
password:
- NotBlank: { groups: [registration] }
- Length: { min: 7, groups: [registration] }
cellphone:
- NotBlank: { groups: [registration] }

不过,显然没有结果。如果失败,我继续使用表单类的setDefaultOptions()方法。

public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'data_class' => 'MyBundleMainBundleEntitySystemUser',
'validation_groups' => array('registration'),
'cascade_validation' => true,
'constraints' => array(
new UniqueEntity(array(
'fields' => array('email')
)),
new UniqueEntity(array(
'fields' => array('username')
))
)
));
}

而且,没有任何运气。目前,我有所有这三个活动在一起,但仍然没有。我分别试了一遍,但又一次,运气不好。

如果有人能指出这里发生了什么,我将不胜感激。我已经为此挣扎了大约6个小时了。

谢谢!

您应该尝试替换:

注释

/**
* @UniqueEntity(fields={"email"}, message="This email address is already used in the system", groups={"registration"})
* @UniqueEntity(fields={"username"}, message="This cellphone number is already used in the system", groups={"registration"})
*/

通过这个:

/**
* @UniqueEntity(fields={"email", "username"}, message="A user with those email and username already exists", groups={"registration"})
*/

Yaml

constraints:
- SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity: email
- SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity: username

通过这个:

constraints:
- SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity:
- email
- username

PHP(Type类)

'constraints' => array(
new UniqueEntity(array(
'fields' => array('email')
)),
new UniqueEntity(array(
'fields' => array('username')
))
)

通过这个:

'constraints' => array(
new UniqueEntity(array(
'fields' => array('email', 'username')
))
)

相关内容

  • 没有找到相关文章

最新更新