An exception occurred while executing a query: SQLSTATE[42883]: Undefined function: 7 ERROR: operator does not exist: json
An exception occurred while executing a query: SQLSTATE[42883]: Undefined function: 7 ERROR: operator does not exist: json
我正在将 PostgreSQL 与 Symfony 结合使用。
我有一个用户 table 如下:
id | email | roles
---------------------------------------
1 | abc@gmail.com | ["ROLE_IVM_USER"]
角色列是 JSON 类型,如上所示。
我想使用以下代码获取具有 ROLE_IVM_USER
角色的用户列表:
$queryBuilder
->andWhere(sprintf('%s.roles IN (:roles)', $rootAlias))
->setParameter('roles', [User::ROLE_IVM_USER]);
导致以下错误:
An exception occurred while executing a query: SQLSTATE[42883]: Undefined function: 7 ERROR: operator does not exist: json = unknown\nLINE 1: ... u0_ WHERE u0_.organization_id = AND u0_.roles IN () OR...\n ^\nHINT: No operator matches the given name and argument types. You might need to add explicit type casts."
我是 PostgreSQL 的新手,让我知道如何让用户具有 ROLE_IVM_USER
的角色
你可以做 LIKE
而不是 IN
:
$queryBuilder
->andWhere(sprintf('%s.roles LIKE :roles', $rootAlias))
->setParameter('roles', '%' . User::ROLE_IVM_USER . '%');
下面是我如何在不使用任何第三方库的情况下获得预期结果的:
$rsm = $this->createResultSetMappingBuilder('u');
$rawQuery = sprintf(
'SELECT %s
FROM public.user u
WHERE u.roles::jsonb ?? :role',
$rsm->generateSelectClause()
);
$query = $this->getEntityManager()->createNativeQuery($rawQuery, $rsm);
$query->setParameter('role', $role);
return $query->getResult();
希望这会对某人有所帮助并节省调查时间
我正在将 PostgreSQL 与 Symfony 结合使用。
我有一个用户 table 如下:
id | email | roles
---------------------------------------
1 | abc@gmail.com | ["ROLE_IVM_USER"]
角色列是 JSON 类型,如上所示。
我想使用以下代码获取具有 ROLE_IVM_USER
角色的用户列表:
$queryBuilder
->andWhere(sprintf('%s.roles IN (:roles)', $rootAlias))
->setParameter('roles', [User::ROLE_IVM_USER]);
导致以下错误:
An exception occurred while executing a query: SQLSTATE[42883]: Undefined function: 7 ERROR: operator does not exist: json = unknown\nLINE 1: ... u0_ WHERE u0_.organization_id = AND u0_.roles IN () OR...\n ^\nHINT: No operator matches the given name and argument types. You might need to add explicit type casts."
我是 PostgreSQL 的新手,让我知道如何让用户具有 ROLE_IVM_USER
你可以做 LIKE
而不是 IN
:
$queryBuilder
->andWhere(sprintf('%s.roles LIKE :roles', $rootAlias))
->setParameter('roles', '%' . User::ROLE_IVM_USER . '%');
下面是我如何在不使用任何第三方库的情况下获得预期结果的:
$rsm = $this->createResultSetMappingBuilder('u');
$rawQuery = sprintf(
'SELECT %s
FROM public.user u
WHERE u.roles::jsonb ?? :role',
$rsm->generateSelectClause()
);
$query = $this->getEntityManager()->createNativeQuery($rawQuery, $rsm);
$query->setParameter('role', $role);
return $query->getResult();
希望这会对某人有所帮助并节省调查时间