如何解决如何在 API Platform / Sylius 中使用输入/数据规范化实现搜索?
我正在 Sylius 1.8.5
中实现产品搜索(基于 API 平台 2.5.7
和 Symfony 4.4.16
)。
product.code
中搜索的基本变体(请参阅下面的代码)正在运行。问题是,product.code
(SKU) 的用户/前端应用输入可能包含点——如果确实如此,用户必须能够忽略它们。示例:数据库中产品的 SKU 为 123.4.567
。输入变量 123.4.567
、1234567
(甚至可能是 1.2.3.4.5.6.7
)应该可以找到产品。
这意味着:我需要
- 标准化输入(例如在自定义
SearchFilter
中,通过在实际搜索之前添加一种预格式化逻辑,例如str_replace('.','','123.4.567');
), - 标准化数据(如
SELECT * FROM sylius_product WHERE REPLACE(code,".","") = "1234567";
)。
如何在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: [ { code: 'partial' } ]
tags: [ name: 'api_platform.filter' ]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。