php - 在 Yii2 搜索中比较匹配的数组索引值

标签 php mysql search yii2


我正在 Yii2 中开发 REST API。在搜索查询中我想实现这个

 WHERE ((`variant_name` LIKE '%size%' AND `variant_value` 
 LIKE '%12%') OR (`variant_name` LIKE '%color%' AND `variant_value` LIKE '%12%'))

variant_name 和variant_value 是在URL 中发送的数组。 我现在使用的对应的Yii2代码如下

$query->andFilterWhere(['or',
['or like', 'pvo.variant_name', $this->variant_name],
['or like', 'pvo.variant_value', $this->variant_value]])
 ->groupBy(['pvo.product_variant_id'])
 ->having(['count(product_variant_id)' => count($this->variant_name)]);
        ;

生成以下 SQL

AND ((`variant_name` LIKE '%size%' OR .`variant_name` LIKE '%color%')
 OR (`variant_value` LIKE '%13%' OR `variant_value` LIKE '%56%')))

但我希望 varaint_name[0] 和 varaint_value[0] 在单括号中与上面提到的 varaint_name[1] 和 varaint_value[1] 具有 AND 条件。 请指导我如何在 Yii2 中进行操作?

更新 1 示例网址 http://localhost:8080/online-malls/api/web/v1/products?expand=product_variant_options&variant_name[0]=size&variant_value[0]=13&variant_name[1]=color&variant_value[1]=56

最佳答案

如果您在 get 中使用数组,那么您应该在 param 中使用数组值,例如:

  $query->andFilterWhere(['or',
    ['or like', 'pvo.variant_name', $this->variant_name[0]],
    ['or like', 'pvo.variant_value', $this->variant_value[0]], 
    ['or like', 'pvo.variant_name', $this->variant_name[1]],
    ['or like', 'pvo.variant_value', $this->variant_value[1]],
    ])
     ->groupBy(['pvo.product_variant_id'])
     ->having(['count(product_variant_id)' => count($this->variant_name)]);

关于php - 在 Yii2 搜索中比较匹配的数组索引值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41312411/

相关文章:

php - 为什么我无法使用 mysqli_fetch_assoc 两次显示相同的结果?

python - 自动完成功能不适用于 Haystack 和 Elasticsearch

php - 缓存/离线网站脚本

javascript - 自动执行查询

mysql - Sakila Schema 未导入 MySQL

mysql - 计算条件为 true 的总行数的百分比

regex - Perl 字符串包含括号

reactjs - VS code 中是否有一个功能,可以打开父组件(又名传递 props 的组件)?

php - 使用符合要求的php显示数据库中的数据

php - 另一个数组内的 stdClass 对象数组