php - Symfony2 外键验证约束

标签 php symfony doctrine-orm

我正在构建一个 Restful API,其中“userId”/“addressId”是 POST/PUT(在其他文件之间),这些字段是数据库中的外键。

我的问题是关于 Symfony 验证组件,因为我想验证这些 ID 是否有效。

我认为,这需要是一个“Doctrine”验证器,因为它需要查询相关表。

我查看了现有的验证器,似乎没有一个涵盖这种情况,实际上它只是一个 Doctrine 验证器(UniqueEntityValidator)

我在这个阶段写了一个自定义验证器..但是有没有其他人发现同样的问题?对于这种情况,是否没有任何标准的 Symfony/Doctrine 验证器可用?

最佳答案

您不必编写自定义验证程序。 Symfony 已经有了对象验证功能。您需要将 type 选项设置为要验证的实体的全名。

# src/AppBundle/Resources/config/validation.yml
AppBundle\Entity\User:
    properties:
        address:
            - Type:
                type: AppBundle\Entity\Address

关于php - Symfony2 外键验证约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18729457/

相关文章:

php - 通过查询生成器对两列求和

php - Symfony2 : imbricate a formType in another formType refer to the foreign key for addAction() method (multiple form Type)

php - 交响乐 2 : How to avoid the sessions table being dropped by doctrine migrations?

symfony - 在symfony(Doctrine)中更新实体时图片被删除

类似于 PHP 的高级 C 实用程序库

javascript - 在表单上使用 jQuery 的 PreventDefault() 后,如何使用 PHP 重定向用户?

php - PDO 回显重复结果

php - 在Symfony 5上使用DateTime约束时,为什么会收到 "This value should be of type string"?

php - zf2.如何处理循环 cli 脚本中 mysql 连接的超时?

php - 学说 2 : Can I get a Reference from a Repository instead of from the Entity Manager?