我將 PostgreSQL 與 Symfony 一起使用。
我有一個用戶表,如下所示:
id | email | roles
---------------------------------------
1 | [email protected] | ["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 = $1 AND u0_.roles IN ($2) OR...\n ^\nHINT: No operator matches the given name and argument types. You might need to add explicit type casts."
我是 PostgreSQL 新手,讓我知道如何讓用戶具有以下角色ROLE_IVM_USER
uj5u.com熱心網友回復:
您可以執行 aLIKE而不是 a IN:
$queryBuilder
->andWhere(sprintf('%s.roles LIKE :roles', $rootAlias))
->setParameter('roles', '%' . User::ROLE_IVM_USER . '%');
uj5u.com熱心網友回復:
以下是我在不使用任何第三方庫的情況下實作預期結果的方法:
$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();
希望這會對某人有所幫助,并將節省數小時的調查時間
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/425500.html
標籤:php json PostgreSQL 交响乐 教义
