【问题标题】:Why is my Symfony2 @UniqueEntity constraint not working at all?为什么我的 Symfony2 @UniqueEntity 约束根本不起作用?
【发布时间】:2012-08-17 07:43:29
【问题描述】:

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

<?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 约束被忽略。我尝试了不同的口味,包括:

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

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

并且,根据此处的 Symfony2 文档:http://symfony.com/doc/current/reference/constraints/UniqueEntity.html

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

我什么都不做,工作。我没有收到预期的表单验证错误,而是收到以下异常:

SQLSTATE[23000]:违反完整性约束:1062 重复条目 'admin@scire.com' 对应键 'UNIQ_D8183973E7927C74'

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

【问题讨论】:

    标签: symfony constraints


    【解决方案1】:

    问题解决如下:

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

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

    【讨论】:

    • 您好 Josef,这似乎是一个老问题,但我遇到了和您一样的问题 - 只是想知道 - 您是否需要,我不知道,创建 某处的验证组?我完全按照您的方式添加了唯一实体字段,仅在电子邮件和手机上,但它仍然给我一个异常而不是验证错误。有什么想法吗?
    • @iLikeBreakfast 您需要在FormTypesetDefaultOptions 方法中声明验证组
    • 为什么这个解决方案与文档中的说明不同?是文档错误,还是这个实现与文档示例有很大不同?symfony.com/doc/2.3/reference/constraints/UniqueEntity.html
    【解决方案2】:

    此问题的另一个原因是,如果您使用表单集合并包含处理关联实体的子表单,则必须在根和所有子表单中将 cascade_validation 设置为 true

    查看Symfony documentation中的提示:

    要激活对 CategoryType 的验证,请将 cascade_validation 选项添加到 TaskType

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\TaskBundle\Entity\Task',
            'cascade_validation' => true,
        ));
    }
    

    更新:

    为了确保您的子实体得到验证,还有更好的方法。 Symfony 正是出于这个原因提供了Valid 约束。来自documentation

    有效
    此约束用于启用对作为被验证对象的属性嵌入的对象的验证。这允许您验证一个对象以及与之关联的所有子对象。

    【讨论】:

      【解决方案3】:

      请注意ignoreNull

      例如要正确使用 pid(可以为 null):

      constraints:
      - Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity:
        fields: [page, pid, position]
        errorPath: page
        groups: [Menu]
        message: "Page already exists with that parent"
        ignoreNull: false
      

      【讨论】:

      • 这就是我的问题的答案。谢谢!
      【解决方案4】:

      尝试添加

      framework:
          validation:
              enable_annotations: true
      

      到您的应用配置

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-09
        • 1970-01-01
        • 2011-02-26
        相关资源
        最近更新 更多