我正在编写一个 Shopware 插件,并且我有以下代码来构建 SQL 查询:
$queryBuilder = $this->container->get('dbal_connection')->createQueryBuilder();
$queryBuilder->select('a.articleID', 'v.optionID', 'v.value')
->from('s_filter_articles', 'a')
->leftJoin('a', 's_filter_values', 'v', 'a.valueID = v.id')
->where('a.articleID IN (:ids)')
->andWhere('v.optionID IN (3, 8)')
->orderBy('a.articleID, v.optionID')
->setParameter(':ids', $relatedIds);
根据以下内容,构建的 SQL 字符串如下$queryBuilder->getSql()
:
SELECT a.articleID, v.optionID, v.value
FROM s_filter_articles a LEFT JOIN s_filter_values v ON a.valueID = v.id
WHERE (a.articleID IN (:ids)) AND (v.optionID IN (3, 8))
ORDER BY a.articleID, v.optionID ASC
当我将 ":ids" 替换为 "1,2,3,4,5" 并执行查询时,所有行都按预期返回。(我确保 $relatedIds 也是“1,2,3,4,5”。)
当我在 PHP (via) 中执行查询时$queryBuilder->execute()->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_ASSOC)
,只返回第一个 ID (1) 的结果。var_dump($result)
显示以下内容:
array(1) { [1]=> array(2) { [0]=> array(2) { ["optionID"]=> string(1) "3" ["value"]=> string(13) "Schwarz Kombi" } [1]=> array(2) { ["optionID"]=> string(1) "8" ["value"]=> string(7) "Schwarz" } } }
为什么这不返回其他 ID (2,3,4,5) 的结果?