# Criteria mit einer Art Having Count

**URL:** <https://forum.shopware.com/t/criteria-mit-einer-art-having-count/100978>\
**Category:** Programmierung\
**Created:** [22. August 2023 um 22:47 UTC](https://forum.shopware.com/t/criteria-mit-einer-art-having-count/100978 "2023-08-22T22:47:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![olli.k](https://avatars.discourse-cdn.com/v4/letter/o/e99b99/32.png) [@olli.k](https://forum.shopware.com/u/olli.k)\
**Post date:** [22. August 2023 um 22:47 UTC](https://forum.shopware.com/t/criteria-mit-einer-art-having-count/100978/1 "2023-08-22T22:47:38Z")

</div>

Hallo,

ich zerbreche mir die ganze Zeit den Kopf…  
Ich will über eine Criteria Bestellungen filtern die mehr als 4 Produkte, aber irgendwie stehe ich auf dem Schlauch, kann mir jemand eine Hilfestellung geben?

Danke liebe Grüße

---

<div class="post-metadata">

**Author:** ![arboro-dg](https://avatars.discourse-cdn.com/v4/letter/a/41988e/32.png) [@arboro-dg](https://forum.shopware.com/u/arboro-dg)\
**Post date:** [7. September 2023 um 09:59 UTC](https://forum.shopware.com/t/criteria-mit-einer-art-having-count/100978/2 "2023-09-07T09:59:46Z")

</div>

Wäre auch an einer Lösung interessiert.

---

<div class="post-metadata">

**Author:** ![AlexGalax](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/alexgalax/32/9925_2.png) [@AlexGalax](https://forum.shopware.com/u/AlexGalax)\
**Post date:** [8. September 2023 um 11:15 UTC](https://forum.shopware.com/t/criteria-mit-einer-art-having-count/100978/3 "2023-09-08T11:15:56Z")

</div>

Es gibt keine `HAVING`-condition im Shopware-DAL. In Doncrine schon, deshalb könntest du dir die vielleicht selber schreiben, oder du kannst über Plain-SQL die IDs holen und dann deren Enities:

```php
$connection = $this->container->get(Connection::class);

$stmt = $this->connection->executeQuery(
<<< 'SQL'
    SELECT `id`
    FROM `product`
    WHERE {condition}
    GROUP BY {column_name(s)}
    HAVING {condition}
    ORDER BY {column_name(s)}
    LIMIT 5
SQL);
$productIds = $stmt->fetchFirstColumn();

$criteria = new Criteria($productIds);
$products = $this->productRepository->search($criteria, $context);

```

---

<div class="post-metadata">

**Author:** ![olli.k](https://avatars.discourse-cdn.com/v4/letter/o/e99b99/32.png) [@olli.k](https://forum.shopware.com/u/olli.k)\
**Post date:** [8. September 2023 um 11:22 UTC](https://forum.shopware.com/t/criteria-mit-einer-art-having-count/100978/4 "2023-09-08T11:22:51Z")

</div>

Hey Alex,

super, Danke dir für die Aufklärung und auch deinen Lösungsvorschlag. Ich schaue mir das mal an, ich glaube, damit kann ich schon arbeiten. Schade, dass es mit dem Shopware - DAL nicht so einfach geht, aber wie man sieht, kann man sich helfen.

Danke dir  
Gruß Oliver
