I'm writing a Shopware plugin, and I have the following code to build an SQL query:
$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);
The built SQL string is the following, according to $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
When I replace ":ids" with "1,2,3,4,5" and execute the query, all rows get returned as expected. (I made sure that $relatedIds is "1,2,3,4,5", too.)
When I execute the query in PHP (via $queryBuilder->execute()->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_ASSOC)
), only results for the first ID (1) are returned. var_dump($result)
shows the following:
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" } } }
Why is this not returning results for other IDs (2,3,4,5)?
I think you need a real array in
->setParameter(':ids', $relatedIds);
for example
$relatedIds = [1,2,3,4,5]