如何解决如何在API平台和Sylius中实现多字段搜索?
我正在 Sylius 1.8.5
中实现产品搜索(基于 API 平台 2.5.7
和 Symfony 4.4.16
)。
product.translations.name
中搜索的基本变体(请参阅下面的代码)正在运行。但是现在我希望能够在多个字段中进行搜索,例如在 product.translations.name
、product.translations.description
和 product.translations.short_description
中。问题是,添加更多条件只会使过滤更加严格,因为它背后的逻辑(在 ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter
中实现)只知道 AND
连接器并在内部使用 andWhere(...)
。
当然,作为一种解决方法,我可以简单地通过自定义覆盖 API 平台的 SearchFilter
并更改我想要的逻辑。但是对于这种情况,也许有更优雅/内置的解决方案?我有这种感觉,应该是这样,因为我想要实现的场景是一个非常常见的场景,并且可能在几乎每个具有(产品)搜索功能的(商店)应用程序中都需要。
在 API 平台/Sylius 中的多个属性/字段中实现(产品)搜索的正确方法是什么?
services.yaml
imports:
- { resource: "filters.yaml" }
filters.yaml
<?xml version="1.0" ?>
...
<resources ...>
<resource class="%sylius.model.product.class%" shortName="Product">
<collectionOperations>
<collectionOperation name="admin_get">
...
</collectionOperation>
<collectionOperation name="shop_get">
<attribute name="method">GET</attribute>
<attribute name="path">/shop/products</attribute>
<attribute name="filters">
<attribute>sylius.api.product_order_filter</attribute>
<attribute>sylius.api.product_taxon_code_filter</attribute>
<attribute>sylius.api.translation_order_name_and_locale_filter</attribute>
<attribute>shop.product_search_filter</attribute> <-- added
</attribute>
</collectionOperation>
<collectionOperation name="admin_post">
...
</collectionOperation>
</collectionOperations>
</resource>
</resources>
filters.yaml
services:
# Filter Services
shop.product_search_filter:
autowire: false
autoconfigure: false
public: false
parent: 'api_platform.doctrine.orm.search_filter'
arguments: [ { translations.name: 'partial',translations.locale: 'exact' } ]
tags: [ name: 'api_platform.filter' ]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。