【发布时间】:2016-06-23 14:08:20
【问题描述】:
可能有标题很接近的线程,但我无法解决这个问题。从 psql 命令行或任何 Postgresql 客户端应用程序调用时,以下查询返回预期结果 但是当我使用 Doctrine 2 实现查询时,我得到以下错误/异常。
raw_data 字段为 JSON 类型。
查询:
SELECT
DISTINCT (((p.raw_data ->> 'company') :: JSONB) ->> 'userId') :: INTEGER
FROM schema.projects p
WHERE p.raw_data :: JSONB ? 'company'
AND (p.raw_data ->> 'company') :: JSONB ? 'userId'
AND (((p.raw_data ->> 'company') :: JSONB) ->> 'userId') :: INTEGER > 0
AND p.is_deleted = FALSE
LIMIT 20;
-- Returns 20 results
教义实施:
public function fetchResulst()
{
$sql = "
SELECT
DISTINCT (((p.raw_data ->> 'company') :: JSONB) ->> 'userId') :: INTEGER
FROM schema.projects p
WHERE p.raw_data :: JSONB ? 'company'
AND (p.raw_data ->> 'company') :: JSONB ? 'userId'
AND (((p.raw_data ->> 'company') :: JSONB) ->> 'userId') :: INTEGER > 0
AND p.is_deleted = FALSE
LIMIT 20
)";
return $this->_em->getConnection()->executeQuery($sql)->fetchAll();
}
响应(异常)
Doctrine \ DBAL \ Exception \ SyntaxErrorException
An exception occurred while executing ' SELECT DISTINCT (((p.raw_data ->> 'company') :: JSONB) ->> 'userId') :: INTEGER FROM listing.projects p WHERE p.raw_data :: JSONB ? 'company' AND (p.raw_data ->> 'company') :: JSONB ? 'userId' AND (((p.raw_data ->> 'company') :: JSONB) ->> 'userId') :: INTEGER > 0 AND p.is_deleted = FALSE LIMIT 20 ) ': SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "$1" LINE 5: WHERE p.raw_data :: JSONB $1 'company' ^
raw_data 有效载荷中“公司”字段的内容如下。
"company": {
"CompanyID": 112233445566,
"URL": null,
"CompanyName": "Some Real Estate Bla bla Contact Office",
"PartyNature": "Contact Office",
"CompanyType": 26,
"SubNature": null,
"Description": "",
"DescriptionLocal": "",
"ImagePath": null,
"Phone1": "+90 987 111 11 11",
"Phone2": "",
"Fax": null,
"ContactEmail": "somauser@example.com",
"NatureID": null,
"PartySubNatureID": null,
"CityID": "123",
"CountyID": "456",
"DistrictID": "789",
"id": 14487,
"userId": 35754
}
版本: Postgresql:PostgreSQL 9.5.2 on x86_64-apple-darwin14.5.0,由 Apple LLVM 版本 7.0.2 (clang-700.1.81) 编译,64 位
学说 - DBAL : v2.5.2
【问题讨论】:
标签: php postgresql orm doctrine-orm