【问题标题】:CakePHP paginate conditionCakePHP 分页条件
【发布时间】:2019-09-19 20:59:13
【问题描述】:

我想搜索从 Excel 复制的多个项目。这些 sku 由空白区域隔开。搜索结果始终为空,因为每个条件都应该是 OR 而不是 AND。如何将分页条件从 AND 更改为 OR?

查询结果

SELECT *, ParentProduct.id FROM parent_products AS ParentProduct WHERE 
ParentProduct.name LIKE '%na301mo%' AND ParentProduct.name LIKE '%gf001bh%' AND 
ParentProduct.name LIKE '%cc302bf%' LIMIT 10

搜索值

na301mo gf001bh cc302bf

控制器

    if (empty($results)) {
        $this->paginate['conditions'] = $this->ParentProduct->resetConditions($searchStr, 'multi-skus');
        $results = $this->paginate('ParentProduct');
    }

型号

    public function resetConditions($searchStr, $opt) {
        if ($opt == ‘multi-skus’) {
            $conditions = array();
            $searchStr = preg_replace('!\s+!', ' ', $searchStr);
            $search_terms = explode(' ', $searchStr);
            foreach ($search_terms as $search_term) {
                $conditions[] = array($this->name . '.sku LIKE' => '%' . $search_term . '%');
            }
        } 
    }

版本。 1.3 倍

请指教。 谢谢

【问题讨论】:

    标签: cakephp


    【解决方案1】:

    您需要在 resetConditions Medhod 中更改 foreach 循环,如下所示:

                foreach ($search_terms as $search_term) {
                    $conditions['OR'][] = array($this->name . '.sku LIKE' => '%' . $search_term . '%');
                }
    

    这将确保所有搜索词都由OR 分隔,而不是AND

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-28
      • 1970-01-01
      • 1970-01-01
      • 2012-09-22
      • 2015-06-26
      • 2011-07-10
      • 2011-09-27
      相关资源
      最近更新 更多