使用Symfony UniqueEntity与原则 [英] Use Symfony UniqueEntity with Doctrine

查看:226
本文介绍了使用Symfony UniqueEntity与原则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让Symfony的UniqueEntity验证器为我的Doctrine实体工作。 Symfony验证器已经挂接起来,然而Symfony的UniqueEntity更具挑战性,显示了这个错误:

I am trying to get Symfony's UniqueEntity validator working for my Doctrine entities. The Symfony validator is already hooked up and working, the UniqueEntity from Symfony\Bridge, however, is more challenging, displaying this error:

PHP Fatal error:  Class 'doctrine.orm.validator.unique' not found in /app/vendor/symfony/validator/Symfony/Component/Validator/ConstraintValidatorFactory.php on line 46

似乎ValidatorFactory正在从Symfony容器请求UniqueEntity验证器,我没有使用,因为我没有使用完整的堆栈symfony。

It appears as if the ValidatorFactory is requesting the UniqueEntity validator from the Symfony container, which I don't have because I am not using full stack symfony.

我的实体看起来像这样:

My entity looks like this:

<?php
namespace App\Model;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints as AssertDoctrine;

/**
 * User
 *
 * @ORM\Table(name="users", uniqueConstraints={@ORM\UniqueConstraint(name="id_UNIQUE", columns={"id"}), @ORM\UniqueConstraint(name="username_UNIQUE", columns={"username"})})
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 * @AssertDoctrine\UniqueEntity("username")
 */
class User extends AbstractEntity
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="username", type="string", length=45, unique=true, nullable=false)
     * @Assert\Length(min=3, max=45)
     */
    protected $username;

    /** ... */
}

其中AbstractEntity是一个映射超类,包括为所有实体提供验证功能的特征:

where AbstractEntity is a mapped superclass including a Trait that provides validation functionality for all entities:

<?php
namespace App\Model\Traits;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Validation;
use App\Utility\Exception\ConstraintViolationException;

/**
 * A trait enabling validation of Doctrine entities to ensure invalid entities
 * don't reach the persistence layer.
 */
trait ValidatorAwareTrait
{
    public function validate()
    {
        $validator = Validation::createValidatorBuilder()
            ->enableAnnotationMapping()
            ->getValidator();

        $violations = $validator->validate($this);

        // Count is used as this is not an array but a ConstraintViolationList
        if (count($violations) !== 0) {
            $message = $violations[0]->getPropertyPath() . ': ' . $violations[0]->getMessage();
            throw new ConstraintViolationException($message);
        }
        return true;
    }
}


推荐答案


  1. DoctrineBridge\Validator\Constraints\UniqueEntity

  2. Symfony\Component\Validator\Constraint

  1. DoctrineBridge\Validator\Constraints\UniqueEntity
  2. Symfony\Component\Validator\Constraint

几个月前我有同样的问题。

I had this same issue a couple of months ago.

简短的回答:您将无法使用UniqueEntity验证器,而不使用某种类型的DI,其中列出了服务'doctrine.orm.validator.unique'并将其转换成Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator。

Short answer: You will not be able to use the UniqueEntity Validator without also using some type of DI which lists a service of 'doctrine.orm.validator.unique' and translates this into Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator.

长回答:您所看到的错误消息是DoctrineBridge\Validator\Constraints\Uniqu eEntity修复Symfony\Component\Validator\Constraint :: validatedBy()。事实上,如果您查看DoctrineBridge\Validator\Constraints\UniqueEntity :: validatedBy()上面的注释,您将看到它声明验证器必须定义为具有此名称的服务,这导致以下事实:该验证器预计将与Symfony DependencyInjection组件一起使用。

Long answer: The error message you are seeing is the result of the fact that DoctrineBridge\Validator\Constraints\UniqueEntity reimplements Symfony\Component\Validator\Constraint::validatedBy(). In fact if you view the comment above DoctrineBridge\Validator\Constraints\UniqueEntity::validatedBy() you will see that it states "the validator must be defined as a service with this name" which is a lead to the fact that this validator is expected to be used with the Symfony DependencyInjection Component.

什么不是立即显而易见的是在错误消息中显示的服务名称实际上是在Doctrine\DoctrineBundle\Resources\Config\Orm.xml中定义

What isn't immediately apparent is that the service name that is being displayed within the error message is actually being defined within Doctrine\DoctrineBundle\Resources\Config\Orm.xml

为orm.xml和dbal.xml配置文件提供链接表示歉意。我的声誉目前不允许。

Apologies for not providing the link to the orm.xml and the dbal.xml configuration files. My reputation does not currently allow for it.

这篇关于使用Symfony UniqueEntity与原则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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