I have a custom endpoint for the admin api which returns some products.
{% block response %}
{% set criteria = {
"includes": {
"product": ["id", "name"],
"cover": ['id']
},
"associations": {
"cover": {},
},
}%}
{% set products = services.repository.search('product', criteria) %}
{% set response = services.response.json({ 'products': products }) %}
{% do hook.setResponse(response) %}
{% endblock %}
When i call this endpoint i want to receive only the products ids and names as described in the criteria. But i get the full object.
I posted an example response below:
{
"apiAlias": "api_feed_data_response",
"products": {
"entity": "product",
"total": 50,
"aggregations": [],
"page": 1,
"limit": 100,
"elements": [
{
"parentId": null,
"childCount": 0,
"taxId": "a4440e5086e14cf793627c9d670694aa",
"manufacturerId": "cc1c20c365d34cfb88bfab3c3e81d350",
"unitId": null,
"active": true,
"displayGroup": "75eef7154e9f22229e54e37b3ca0e54b",
"manufacturerNumber": null,
"ean": null,
"sales": 0,
...
}
How can i get only the requested fields (id, name) ?
Starting with 6.5 of shopware it will be possible with:
{% block response %}
{% set criteria = {
"fields": {
"product": ["id", "name"],
"cover": ['id']
},
"associations": {
"cover": {},
},
}%}
...
{% endblock %}
So you should use fields
instead of includes
.
The difference between those two is that includes
are only filtered out at the CRUD-API level,but before the response is encoded in the API layer the entities contain all fields. As you have a custom API-endpoint the filtering does not work in this case.
The fields
are filtered out directly at the DB level, so that only the defined fields are fetched from the database, that means that your response in your custom API endpoint will also only include those fields.