简体   繁体   English

Shopware 6 Plugin Api 获取带有选定字段的产品数据以减少响应负载

[英]Shopware 6 Plugin Api get Product data with selected field to reduce response payload

We created a plugin and now we want to get selected fields only on the response, to minimize the response payload.我们创建了一个插件,现在我们只想在响应中获取选定的字段,以最小化响应负载。

We are aware that criteria "includes" will help based on the URL https://developer.shopware.com/docs/guides/integrations-api/general-concepts/search-criteria我们知道标准“包括”将根据 URL https://developer.shopware.com/docs/guides/integrations-api/general-concepts/search-criteria有所帮助

In the Array of $criteria we can see what "includes" is added like filter but does not give the selected fields$criteria数组中,我们可以看到像过滤器一样添加了“包含”,但没有给出所选字段

Please help what I am doing wrong?请帮助我做错了什么?

EXample例子

use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
$Criteria = new Criteria();
$Criteria->addFilter(new EqualsFilter('id', $productId));
$Criteria->setIncludes(array('product-alias' => array('name')));
$productRepo = $this->productRepository->search($Criteria, $context);

I have tried我努力了

$Criteria->setIncludes(array('product' => array('name')));
$Criteria->setIncludes(array('name'));

The includes collection is only used when serializing the objects for the response json, as you can find in the JsonApiEncoder . includes集合仅在序列化响应 json 的对象时使用,您可以在JsonApiEncoder中找到。 It's not in effect when fetching mapped objects internally, as it could lead to conflicts with only partially mapped objects.它在内部获取映射对象时无效,因为它可能导致仅与部分映射对象发生冲突。 You could of course just use the JsonApiEncoder internally to encode a collection of entities to receive a response object including a subset of the data.您当然可以在内部使用JsonApiEncoder对实体集合进行编码,以接收包含数据子集的响应 object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM