如何在Bolt的setcontent中对select字段的已选值执行过滤操作
实现方案
你配置的city字段是关联regions内容类型的关联选择字段,Bolt存储这类字段时会默认保存关联记录的主键ID,你可以按以下方案实现过滤:
方案1:URL传城市ID(推荐,性能更高)
如果你的查询参数直接传对应城市(regions记录)的ID,直接在条件里匹配即可。建议把多个过滤条件合并后统一查询,避免重复编写setcontent逻辑:
{# 初始化查询条件数组 #} {% set filterParams = {} %} {# 原有付费筛选逻辑 #} {% if app.request.query.get('onlyPaid') == 'true' %} {% set filterParams = filterParams|merge({ is_paid: 1 }) %} {% endif %} {# 新增城市筛选逻辑 #} {% set reqCityId = app.request.query.get('cityId') %} {% if reqCityId is not empty and reqCityId is numeric %} {% set filterParams = filterParams|merge({ city: reqCityId }) %} {% endif %} {# 统一执行查询 #} {% setcontent records = 'internships' where filterParams limit 6 %}
方案2:URL传城市名称
如果你的查询参数传的是城市中文名称,需要先查询匹配的regions记录拿到ID,再作为过滤条件:
{% set filterParams = {} %} {# 付费筛选逻辑保持不变 #} {% if app.request.query.get('onlyPaid') == 'true' %} {% set filterParams = filterParams|merge({ is_paid: 1 }) %} {% endif %} {# 按城市名筛选 #} {% set reqCityName = app.request.query.get('cityName') %} {% if reqCityName is not empty %} {# 先查对应城市的regions记录 #} {% setcontent targetCity = 'regions' where { title: reqCityName } return single %} {% if targetCity is not empty %} {% set filterParams = filterParams|merge({ city: targetCity.id }) %} {% endif %} {% endif %} {% setcontent records = 'internships' where filterParams limit 6 %}
内容的提问来源于stack exchange,提问作者DebugAndConquer
相关产品推荐
相关产品推荐

