Limit association query via Shopware 6 Admin API

17 views Asked by At

I want to fetch layouts together with the categories, products, ... they are assigned to.

This can be done by adding an association to a query on the pagerepository (this.repositoryFactory.create('cms_page');)

const criteria = new Criteria(this.page, this.limit);
criteria.addAssociation('categories');

in the listCriteria method.

There might be many matches, so I want to get only the first 5 associated entities.

I tried

criteria.addAssociation('categories').setLimit(5);

But that would limit the full query.

How can I fine-tune this associated query?

1

There are 1 answers

0
Alex On

Just figured it out after posting:

This works after adding the association:

criteria.getAssociation('categories').setLimit(5);

EDIT: Even this single line is enough: The association does not need to be added first. getAssociation() does this implicitly.