简体   繁体   English

为什么我的'where in'Dictrtrine DQL查询没有返回预期的结果?

[英]Why does my 'where in' Doctrine DQL query not return expected results?

I have the following DQL query: 我有以下DQL查询:

$queries_query = $this->_em->createQuery('SELECT q FROM \Entities\Query q INNER JOIN q.store s WHERE s.tvRegionId IN (?1)');
$queries_query->setParameter(1, $tv_region_ids);

It only returns results that match the first value in the where clause. 它仅返回与where子句中的第一个值匹配的结果。 The variable $tv_region_ids contains comma separated values. 变量$ tv_region_ids包含逗号分隔值。 If I change that variable to an array I get the following message: 如果我将该变量更改为数组,我会收到以下消息:

Notice: Array to string conversion 注意:数组到字符串转换

Can anyone shed some light on my problem please. 任何人都可以对我的问题有所了解。 I am completely stumped. 我完全难过了。

You cannot pass a string into DQL for a IN type query. 对于IN类型查询,您不能将字符串传递给DQL。 You need to pass in an array. 你需要传入一个数组。 Because DQL works very much like prepared statements, an IN query is treated as an expression and raw data must be provided into the setParameter function. 因为DQL与预处理语句非常相似,所以IN查询被视为表达式,并且必须将原始数据提供给setParameter函数。

Example

$tv_region_ids = [16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1];

$queries_query = $this->_em->createQuery('SELECT q FROM \Entities\Query q INNER JOIN q.store s WHERE s.tvRegionId IN (?1)');
$queries_query->setParameter(1, $tv_region_ids);

Converting your String (Just Incase) 转换你的字符串(只是包含)

$string = "16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1";

$tv_regions_ids = explode(",", $string);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM