dqthn68688 2016-12-24 10:22
浏览 139
已采纳

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


I am developing REST API in Yii2. In search query i want to achieve this

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

the variant_name and variant_value are an array sent in the URL. The corrsponding Yii2 code i am using right now is as follows

$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)]);
        ;

Which is generating the following SQL

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

But i want varaint_name[0] and varaint_value[0] in single bracket having AND condition with varaint_name[1] and varaint_value[1] like mentioned above. Kindly guide me how should i go about it in Yii2?

Update 1 Sample URL 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

  • 写回答

1条回答 默认 最新

  • douzhoulei8959 2016-12-24 13:58
    关注

    If you are using an array in get then you should use the array value in param eg:

      $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)]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化