为什么我的Symfony2 @UniqueEntity约束不工作? [英] Why is my Symfony2 @UniqueEntity constraint not working at all?

查看:168
本文介绍了为什么我的Symfony2 @UniqueEntity约束不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程式中有以下实体类别:

I have the following entity class in my application:

<?php

namespace ...;

// use ...
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;;
// ...

/**
 * @ORM\Table(name="sc_user")
 * @ORM\Entity(repositoryClass="...\UserRepository")
 * @ORM\HasLifecycleCallbacks()
 * @UniqueEntity(fields={"email", "username"})
 */
class User implements UserInterface, \Serializable, EquatableInterface
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string $email
     *
     * @ORM\Column(name="email", type="string", length=255, unique=true)
     *
     * @Assert\NotBlank(groups={"registration"})
     * @Assert\Email(groups={"registration"})
     */
    private $email;

    /**
     * @var string $username
     *
     * @ORM\Column(name="username", type="string", length=32, unique=true)
     *
     * @Assert\NotBlank(groups={"registration"})
     */
    private $username;

    // ...
}

@UniqueEntity约束被忽略。我尝试了不同的口味,包括:

The @UniqueEntity constraint is being ignored. I tried different flavors including:

@UniqueEntity(fields={"email", "username"})

@UniqueEntity(fields={"email"})
@UniqueEntity(fields={"username"})


$ b b

,并按照Symfony2文档, http://symfony.com/doc/current /reference/constraints/UniqueEntity.html

@UniqueEntity("email")
@UniqueEntity("username")


SQLSTATE [23000]:完整性约束违反:1062重复entry
'admin@scire.com'for key'UNIQ_D8183973E7927C74'

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'admin@scire.com' for key 'UNIQ_D8183973E7927C74'

这是错误的!有没有人知道如何解决这个问题?

This is just wrong! Does anyone know how to fix this issue?

推荐答案

问题解决如下:

@UniqueEntity(fields={"email"}, groups={"registration"})
@UniqueEntity(fields={"username"}, groups={"registration"})

注册组缺失,我需要将它们分成两个单独的注释。

The registration groups were missing, and I needed to separate them into two separate annotations.

这篇关于为什么我的Symfony2 @UniqueEntity约束不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆