thinkphp where 与 map 为and 关系 ,多个map为or 关系

thinkphp where 与 map 为and 关系 ,多个map为or 关系 

上代码:

public static function makeGoodsCateMap($model,$cateArr1,$cateArr2,$cateArr3,$alias='')
    {
        // 收集分类数组
        $cateArr1 = json_decode($cateArr1);
        $cateArr2 = json_decode($cateArr2);
        $cateArr3 = json_decode($cateArr3);
        $map = [];
        foreach ($cateArr1 as $index=>$cate1){
            if(empty($cate1)) continue; // 不需要,一级为0
            if( !isset($cateArr2[$index]) ) continue; // 数据不完整
            if( !isset($cateArr3[$index]) ) continue; // 数据不完整
            $cate2 = $cateArr2[$index];
            $cate3 = $cateArr3[$index];
            // 第二级是否为0
            if($cate2 == 0){
                $map[] = ["{$alias}.category_id_one",'=',$cate1];
            }else{
                // 第三级 是否为0
                if($cate3 == 0){
                    $map[] = ["{$alias}.category_id_two",'=',$cate2];
                }else{
                    $map[] = ["{$alias}.category_id_three",'=',$cate3];
                }
            }
        }
        return $model->where(function ($query) use ($map) {
            foreach ($map as $condition) {
                $query->whereOr($condition[0], $condition[1], $condition[2]);
            }
        });
    }

组装条件为

 return $model->where(function ($query) use ($map) {
            foreach ($map as $condition) {
                $query->whereOr($condition[0], $condition[1], $condition[2]);
            }
        });

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值