Symfony UniqueEntity 异常

标签 symfony doctrine-orm

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

我已阅读 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/

相关文章:

php - Symfony2 Doctrine 元数据缓存与 Redis 问题

symfony - 如何将图像添加到 Sylius 中的自定义实体?

php - Symfony 4 生产模式错误处理页面内存耗尽

ajax - 用于调用 ajax 的按钮的 Symfony Crawler

symfony - 在 Symfony2 的查询中使用自定义 DQL 函数

mysql - 我的程序的数据库设计是否存在循环引用问题?

symfony - 提交和开始事务有什么作用?

php - Doctrine在使用单表继承时无法生成模式?

php - 使用类表继承从存储库中获取子类

symfony - 多引用 fixture 范围@user {1..10}