从symfony 2中的教义2实体中提取约束 [英] Extract constraints form Doctrine 2 entity in symfony 2

查看:74
本文介绍了从symfony 2中的教义2实体中提取约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了将字段级别的约束保持在中心位置(而不是以每种形式复制它),我在实体中添加了约束。如下所示(让我们说它是用户实体的字段之一):

To keep the field level constraints at a central place (not replicate it in each form), I added the constraints in the entity. Like below (lets say its one of the fields of a user entity):

 /**
 * @var string
 *
 * @ORM\Column(name="email", type="string", length=255, nullable=false)
 *
 * @Constraints\NotBlank(
 *      groups={"register", "edit"},
 *      message="email cannot be blank."
 * )
 * @Constraints\Email(
 *      groups={"register", "edit"},
 *      message="Please enter a valid email address."
 * )
 *
 * @Expose
 * @Groups({"list", "details"})
 */
private $email;

现在,我需要一种方法来公开每个字段的验证约束,这是 Symfony\ 组件\验证器\约束。有没有一种方法可以获取实体中所有字段的所有约束,例如:

Now I need a way to expose this validation constraints for each field which is an annotation of "Symfony\Component\Validator\Constraints". Is there a way that I can get all the constraints for all fields in the entity, like:

$em->getValidationConstraints('MyBundle:EntityUser'); //em is the entity manager
//and it returns me all the fields with its name, type and any constraints  
//attached to it as any array

提前感谢。

推荐答案

收集信息



在解决问题之前,最好先了解您在说什么并收集一些信息。

Gather Information

Before fixing a problem, it's good to know what you are talking about and gather some information.

Doctrine是一个ORM,可以在数据库和对象之间完成某些工作。它与Symfony2 Validator组件完成的验证无关。因此,除了 $ em 之外,您还需要其他东西。

Doctrine is an ORM, something that does nice things between a database and an object. It has nothing to do with validation, that is done by the Symfony2 Validator Component. So you need something else than the $em.

一个类的所有约束都称为元数据,它们通常存储在 Symfony\Component\Validator中\映射\ClassMetadata 。我们必须找到一个接受类名称并返回 ClassMetadata 实例的类。

All constraints of a class are called 'metadata' and they are usually stored in Symfony\Component\Validator\Mapping\ClassMetadata. We have to find a class which accepts the name of a class and returns a ClassMetadata instance.

约束,Symfony2 Validator组件使用加载器

To load the constraints, the Symfony2 Validator component uses loaders.

我们可以看到有一个 Symfony\组件\Validator\Mapping\ClassMetadataFactory 。工厂总是用于根据特定的参数构建类。在这种情况下,我们知道它将创建一个 ClassMetadata ,并且可以看到它接受一个类名。我们必须调用 ClassMetadataFactory :: getMetadataFor

We can see that there is a Symfony\Component\Validator\Mapping\ClassMetadataFactory. A factory is always used to build a class from a specific argument. In this case, we know it will create a ClassMetadata and we can see that it accepts a classname. We have to call ClassMetadataFactory::getMetadataFor.

但是我们看到它需要一些加载器。我们不会为初始化该工厂做大的工作,如何使用服务容器呢?我们可以看到该容器具有 validator.mapping.class_metadata_factory 服务,这正是我们需要的类。

But we see it needs some loaders. We aren't going to do the big job of initializing this factory, what about using the service container? We can see that the container has a validator.mapping.class_metadata_factory service, which is exactly the class we need.

现在我们拥有了所有这些,让我们使用它:

Now we have all of that, let's use it:

// ... in a controller (maybe a seperated class is beter...)
public function someAction()
{
    $metadataFactory = $this->get('validator.mapping.class_metadata_factory');
    $metadata = $metadataFactory->getMetadataFor('Acme\DemoBundle\Entity\EntityUser');
}

现在我们有了元数据,我们只需要将其转换为数组即可:

Now we have the metadata, we only need to convert that to an array:

// ...
$propertiesMetadata = $metadata->properties;
$constraints = array();

foreach ($propertiesMetadata as $propertyMetadata) {
    $constraints[$propertyMetadata->name] = $property->constraints;
}

现在, $约束是包含所有字段及其约束数据的数组,例如:

Now, $constraints is an array with all fields and their constraint data, something like:

Array (
    ...

    [email] => Array (
        [0] => <instance of NotBlank>
        [1] => <instance of Email>
    ),
)

这篇关于从symfony 2中的教义2实体中提取约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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