Cakephp分页如何使用或条件

时间:2017-11-08 13:58:30

标签: php cakephp pagination cakephp-2.0 paginator

我正在尝试使用paginator组件中的搜索来过滤配方。但它只搜索食谱表的标题栏。我希望它还可以搜索配方表的主体列和标签列。

在控制器中

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

当我查看$this->Paginator->settings['conditions']时,它看起来像这样

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

我想扩展条件,以便更多地查看配方表的多个列,例如Recipe.title LIKERecipe.body LIKERecipe.tags LIKE

1 个答案:

答案 0 :(得分:0)

您需要在模型中使用自定义功能。

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;
}