Search code examples
phpshopware

Shopware 6 - Using DAL to filter collections inside entities


I want to get all Product Streams, which have at least one category. I am trying to do this:

$criteria = new Criteria();
$criteria->addAssociation('categories');
$criteria->addFilter(new NotFilter(
    NotFilter::CONNECTION_OR,
    [
        new EqualsFilter('categories', null)
        // Also tried this:
        // new EqualsFilter('categories', ['id' => null])
    ]
));
$productStreams = $productStreamRepo->search($criteria, $this->context);

But this always results in the following error:

Call to a member function buildAccessor() on null

I always get this error, no matter what I do. Am I doing something wrong or how would I archive this?


Solution

  • It is actually quite simple. Instead of adding the filter like this:

    new EqualsFilter('categories', null);
    

    You actually need to do it like this:

    new EqualsFilter('categories.id', null);
    

    You can also chain this multiple times, which allows you to filter by subcollections:

    new EqualsFilter('categories.salesChannel.id', null);