将 Symfony UniqueEntity 与 Doctrine 一起使用 [英] Use Symfony UniqueEntity with Doctrine
问题描述
我正在尝试让 Symfony 的 UniqueEntity 验证器为我的 Doctrine 实体工作.Symfony 验证器已经连接并工作,但是来自 SymfonyBridge 的 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 SymfonyBridge, 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.
我的实体如下所示:
<?php
namespace AppModel;
use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;
use SymfonyBridgeDoctrineValidatorConstraints as AssertDoctrine;
/**
* User
*
* @ORMTable(name="users", uniqueConstraints={@ORMUniqueConstraint(name="id_UNIQUE", columns={"id"}), @ORMUniqueConstraint(name="username_UNIQUE", columns={"username"})})
* @ORMEntity(repositoryClass="AppRepositoryUserRepository")
* @AssertDoctrineUniqueEntity("username")
*/
class User extends AbstractEntity
{
/**
* @var integer
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @var string
*
* @ORMColumn(name="username", type="string", length=45, unique=true, nullable=false)
* @AssertLength(min=3, max=45)
*/
protected $username;
/** ... */
}
其中 AbstractEntity 是一个映射超类,包括为所有实体提供验证功能的 Trait:
where AbstractEntity is a mapped superclass including a Trait that provides validation functionality for all entities:
<?php
namespace AppModelTraits;
use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorValidation;
use AppUtilityExceptionConstraintViolationException;
/**
* 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;
}
}
推荐答案
参考:
几个月前我也遇到过同样的问题.
I had this same issue a couple of months ago.
简答:如果不使用某种类型的 DI,您将无法使用 UniqueEntity
验证器,其中列出了 doctrine.orm.validator.unique
的服务和将其翻译成 SymfonyBridgeDoctrineValidatorConstraintsUniqueEntityValidator
.
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 SymfonyBridgeDoctrineValidatorConstraintsUniqueEntityValidator
.
长答案:您看到的错误消息是 DoctrineBridgeValidatorConstraintsUniqueEntity
重新实现 SymfonyComponentValidatorConstraint::validatedBy()代码>.实际上,如果您查看
DoctrineBridgeValidatorConstraintsUniqueEntity::validatedBy()
上方的评论,您会看到它声明验证器必须定义为具有此名称的服务",这是一个线索事实上,这个验证器预计将与 Symfony DependencyInjection
组件一起使用.
Long answer: The error message you are seeing is the result of the fact that DoctrineBridgeValidatorConstraintsUniqueEntity
reimplements SymfonyComponentValidatorConstraint::validatedBy()
. In fact if you view the comment above DoctrineBridgeValidatorConstraintsUniqueEntity::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.
不明显的是,错误消息中显示的服务名称实际上是在 DoctrineDoctrineBundleResourcesConfigOrm.xml
What isn't immediately apparent is that the service name that is being displayed within the error message is actually being defined within DoctrineDoctrineBundleResourcesConfigOrm.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 与 Doctrine 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!