简体   繁体   English

Cakephp分页如何使用或条件

[英]Cakephp Pagination how to use or condition

I am trying to filter recipes using a search in a paginator component. 我正在尝试使用分页器组件中的搜索来过滤配方。 But it only searches in the title column of the recipes table. 但它仅在配方表的标题列中搜索。 I want it to also search the body column and tags columns of the recipes table. 我希望它也搜索配方表的正文列和标签列。

In controller 在控制器中

$this->Paginator->settings = $this->paginate;
$this->Paginator->settings['limit']=$this->pageLimit;
$this->Paginator->settings['maxLimit']=$this->maxLimit;
$this->Paginator->settings['conditions'] = $this->Recipe->parseCriteria($this->Prg->parsedParams());

when I looked into the $this->Paginator->settings['conditions'] it looks like this 当我查看$this->Paginator->settings['conditions']它看起来像这样

Array
(
    [Recipe.title LIKE] => %chocolate%
)

I want to extend the conditions to also look into multiple columns of the recipe table more like Recipe.title LIKE , Recipe.body LIKE and Recipe.tags LIKE . 我想扩展条件,以便也查看配方表的多个列,例如Recipe.title LIKERecipe.body LIKERecipe.tags LIKE

You need to use custom function in your model. 您需要在模型中使用自定义函数。

public $filterArgs = array(
    'filter' => array(
        'type' => 'query',
        'method' => 'orConditions'
    ),
);

public function orConditions($data = array()) {
    $filter = $data['filter'];
    $condition = array(
        'OR' => array(
            $this->alias . '.title LIKE' => '%' . $filter . '%',
            $this->alias . '.body LIKE' => '%' . $filter . '%',
            $this->alias . '.body LIKE' => '%' . $filter . '%',
        )
    );
    return $condition;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM