thinkphp 多条件查询 不起作用 = like

thinkphp 多条件查询 不起作用 = like

码农世界 2024-05-21 后端 65 次浏览 0个评论

 不起作用的代码:  ( where(['category_id' => $item['id']]) 没起作用  )  传递参数

$model->where('product_name', 'like', "%$productName%") 不起作用

public function cateProductPage()
{
    $builder = new Builder(new CategoryModel());
    $builder->setDefaultSort('sort,ASC');
    $builder->setFilter(['pid' => -1]);
    $lists = $builder->selectQuery(function (BaseQuery $query) {
    })->select();
    $productName = $this->request->post('product_name', '');
    $model =  new ProductModel();
    if ($lists->toArray()) {
        foreach ($lists as &$value) {
            // 二级分类 名
            $value['c_names'] = (new CategoryModel())->where(['pid' => $value['id']])->field('id,name')->select();
            // 二级分类商品
            foreach ( $value['c_names'] as &$item){
                
                $item['good'] =  $model->where(['category_id' => $item['id']]);
                if (!empty($productName)) {
                    $model->where('product_name', 'like', "%$productName%")->select();
                }
                $item['good']  = $model->select();
                // 提取标签中的 ‘约500g/份 约10-20 只'  约的展示 与 标签不展示在一起了
                foreach ($item['good'] as &$index) {
                    $pattern = '/约[^,]*/';
                    preg_match_all($pattern, implode(', ',$index['tags']), $matches);
                    $index['tag'] =  explode(',', implode(",", $matches[0]));
                }
            }
            $where = [];
            $user = $this->request->getUser();
            if ($user) {
                $where[] = ['is_view', 'find in set', $user['user_type']];
            }
            $pro = (new ProductModel())->with(['sku', 'ranks', 'category'])->whereRaw('concat(\' \',category_id,\' \') like "% ' . $value['id'] . ' %"')->where('status', '10')->where($where)->order([
                'sort' => 'ASC',
                'id' => 'DESC',
            ])->select();
            $value['product'] = $pro;
        }
        unset($value);
        unset($item);
        unset($index);
    }
    return $lists;
}

 日志:thinkphp 多条件查询 不起作用 = like

起作用的代码:

public function cateProductPage()
    {
        $builder = new Builder(new CategoryModel());
        $builder->setDefaultSort('sort,ASC');
        $builder->setFilter(['pid' => -1]);
        $lists = $builder->selectQuery(function (BaseQuery $query) {
        })->select();
        $productName = $this->request->post('product_name', '');
        $model =  new ProductModel();
        if ($lists->toArray()) {
            foreach ($lists as &$value) {
                // 二级分类 名
                $value['c_names'] = (new CategoryModel())->where(['pid' => $value['id']])->field('id,name')->select();
                // 二级分类商品
                foreach ( $value['c_names'] as &$item){
                    $item['good'] =  $model->where(['category_id' => $item['id']])->where(function ($query) use($productName) {
                        if(!empty($productName)){
                            $query->where('product_name', 'like', "%$productName%");
                        }
                    })->select();
                    // 提取标签中的 ‘约500g/份 约10-20 只'  约的展示 与 标签不展示在一起了
                    foreach ($item['good'] as &$index) {
                        $pattern = '/约[^,]*/';
                        preg_match_all($pattern, implode(', ',$index['tags']), $matches);
                        $index['tag'] =  explode(',', implode(",", $matches[0]));
                    }
                }
                $where = [];
                $user = $this->request->getUser();
                if ($user) {
                    $where[] = ['is_view', 'find in set', $user['user_type']];
                }
                $pro = (new ProductModel())->with(['sku', 'ranks', 'category'])->whereRaw('concat(\' \',category_id,\' \') like "% ' . $value['id'] . ' %"')->where('status', '10')->where($where)->order([
                    'sort' => 'ASC',
                    'id' => 'DESC',
                ])->select();
                $value['product'] = $pro;
            }
            unset($value);
            unset($item);
            unset($index);
        }
        return $lists;
    }

 日志:thinkphp 多条件查询 不起作用 = like

转载请注明来自码农世界,本文标题:《thinkphp 多条件查询 不起作用 = like》

百度分享代码,如果开启HTTPS请参考李洋个人博客
每一天,每一秒,你所做的决定都会改变你的人生!

发表评论

快捷回复:

评论列表 (暂无评论,65人围观)参与讨论

还没有评论,来说两句吧...

Top