You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Symfony中Doctrine通过UUID数组查询用户返回空数组问题

解决Symfony+Doctrine通过UUID数组检索用户返回空的问题

可行解决方案

方法1:用QueryBuilder显式构建IN查询

findBy在处理UUID对象数组时可能存在隐性类型映射问题,直接使用QueryBuilder可以确保参数类型正确绑定:

// 转换字符串UUID为UUID对象数组
$uuidIds = array_map(fn(string $id) => Uuid::fromString($id), $ids);

// 通过QueryBuilder执行查询
$users = $this->getDoctrine()->getRepository(User::class)
    ->createQueryBuilder('u')
    ->where('u.id IN (:ids)')
    ->setParameter('ids', $uuidIds, \Doctrine\DBAL\Types\UuidType::NAME)
    ->getQuery()
    ->getResult();

方法2:直接传递字符串数组(需数据库存储为字符串格式)

如果数据库中UUID是以字符串形式存储的,可直接用字符串数组构建查询:

$users = $this->getDoctrine()->getRepository(User::class)
    ->createQueryBuilder('u')
    ->where('u.id IN (:ids)')
    ->setParameter('ids', $ids)
    ->getQuery()
    ->getResult();

方法3:升级Doctrine ORM版本

旧版本Doctrine ORM对findBy处理UUID数组的支持存在bug,升级到最新稳定版可解决该问题。同时确保config/packages/doctrine.yaml中已正确注册UUID类型:

doctrine:
    dbal:
        types:
            uuid: '%doctrine.dbal.uuid_type%'

问题原因分析

单独传递单个UUID对象时,Doctrine能正确完成类型转换;但传递UUID对象数组时,内部类型转换器未正确处理数组中每个元素的格式,导致生成的SQL查询参数与数据库存储值不匹配,最终返回空结果。

内容的提问来源于stack exchange,提问作者Geod

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 17:01:12