gpt4 book ai didi

Symfony UniqueEntity 异常

转载 作者:行者123 更新时间:2023-12-03 18:03:51 25 4
gpt4 key购买 nike

好吧,我知道这已经被问了一百万次了,但这里是。

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

/**
* SystemUser
*
* @ORM\Table(name="systemuser")
* @ORM\Entity(repositoryClass="MyBundle\MainBundle\Entity\Repository\SystemUserRepository")
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="discr", type="integer")
* @ORM\DiscriminatorMap({"0" = "SystemUser", "1" = "SchoolAdmin", "2" = "Teacher", "3" = "Student", "4" = "Guardian", "5" = "SystemAdmin"})
* @ORM\HasLifecycleCallbacks()
* @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 .我试过没有它,同样的异常(exception)。我也试过没有 groups={"registration"}标志,我不确定是否必须在某处指定和声明“注册”组(我确实检查了文档但找不到任何内容)。事实上,我不知道在这种情况下验证组实际上有什么用,但我还是尝试了。

在那里失败了,我开始尝试validation.yml ...
MyBundle\MainBundle\Entity\SystemUser:
constraints:
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: email
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: 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' => 'MyBundle\MainBundle\Entity\SystemUser',
'validation_groups' => array('registration'),
'cascade_validation' => true,
'constraints' => array(
new UniqueEntity(array(
'fields' => array('email')
)),
new UniqueEntity(array(
'fields' => array('username')
))
)
));
}

此外,没有任何运气。目前我将所有 3 个都激活了,但仍然没有。我分别尝试了所有这些,但还是没有运气。

如果有人能指出这里发生了什么,我将不胜感激。现在已经为此苦苦挣扎了大约 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"})
*/

亚姆
constraints:
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: email
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: username

这样 :
constraints:
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity:
- email
- username

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

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

关于Symfony UniqueEntity 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20982036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com