# DAL associations filtern

**URL:** <https://forum.shopware.com/t/dal-associations-filtern/61190>\
**Category:** Programmierung\
**Created:** [20. August 2019 um 08:00 UTC](https://forum.shopware.com/t/dal-associations-filtern/61190 "2019-08-20T08:00:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pino](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/pino/32/14373_2.png) [@pino](https://forum.shopware.com/u/pino)\
**Post date:** [20. August 2019 um 08:00 UTC](https://forum.shopware.com/t/dal-associations-filtern/61190/1 "2019-08-20T08:00:42Z")

</div>

tach zusammen,

ich versuche vie DAL einträge zu filter.  
speziel suche ich einträge anhand vom aktuellen saleChannel und items die aktiv sind.

die tabellen sind in etwa so aufgebaut.

- item&nbsp;(items mit stammdaren, wie name, acrive, position usw.)
- item\_sale\_channel (beziehung von sale channel und item)

so sehen meien einträge aus, wenn ich nach allen suche.

```
[85c2f6809c8e4744b75941f3e0cf3ca6] => ItemsSalesChannelEntity Object
(
    [id:protected] => 85c2f6809c8e4744b75941f3e0cf3ca6
    [itemId:protected] => 8315b34b35614f7ea2471e048adc9d85
    [salesChannelId:protected] => 1a78aaa7eb4e4df8b883ed08a8220b02
    [priority:protected] => 1
    [item:protected] => ItemEntity Object
        (
            [id:protected] => 8315b34b35614f7ea2471e048adc9d85
            [active:protected] => 
            [position:protected] => 1 
            ...
        )

    [salesChannel:protected] => Shopware\Core\System\SalesChannel\SalesChannelEntity Object
        (
            [id:protected] => 1a78aaa7eb4e4df8b883ed08a8220b02
            [typeId:protected] => 8a243080f92e4c719546314b577cf82b
            [languageId:protected] => 2fbb5fe2e29a4d70aa5854ce7ce3e20b
            ...
        )
)

```

mein criteria&nbsp;sieht so aus, wenn ich nach aktive items suche:

```
$criteria = new Criteria();
$criteria->addAssociationPath('item')
    ->addAssociationPath('salesChannel')
    ->addFilter(new EqualsFilter(
        'item_sale_channel.salesChannelId',
        $salesChannelContext->getSalesChannel()->getId()
    ))
    ->addFilter(new EqualsFilter('item.active', true));

$result = $this->itemSaleChannelRepository->search($criteria, $salesChannelContext->getContext());

```

error:

```
[▼
  "exception" => UnmappedFieldException^ {#2358 ▼
    #parameters: [▶]
    #message: "Field "active" in entity "item" was not found."
    #code: 0
    #file: "/var/www/shopware6/platform/src/Core/Framework/DataAbstractionLayer/Dbal/EntityDefinitionQueryHelper.php"
    #line: 174
    trace: {▶}
  }
]

```

ist es möglich per DAL in den “associations” zu suchen?

&nbsp;

---

<div class="post-metadata">

**Author:** ![kd-wbn](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/kd-wbn/32/20225_2.png) [@kd-wbn](https://forum.shopware.com/u/kd-wbn)\
**Post date:** [29. August 2024 um 09:02 UTC](https://forum.shopware.com/t/dal-associations-filtern/61190/2 "2024-08-29T09:02:48Z")

</div>

Für alle, die nach dem Thema suchen:  
mittels `getAssociation` statt `addAssociation` lassen sich die Associations selbst filtern

Im Beispiel von pino könnte es so funktionieren

```PHP
$criteria = new Criteria();

$criteria
    ->addAssociationPath('salesChannel')
    ->addFilter(new EqualsFilter(
        'item_sale_channel.salesChannelId',
        $salesChannelContext->getSalesChannel()->getId()
    ))

$criteria->getAssociation('item')->addFilter(new EqualsFilter('active', true));

$result = $this->itemSaleChannelRepository->search($criteria, $salesChannelContext->getContext());

```

Unter „Filter associations“

> **[Reading Data | Shopware Documentation](https://developer.shopware.com/docs/guides/plugins/plugins/framework/data-handling/reading-data.html#associations)**
>
> Documentation for Shopware developers
