brenoroosevelt/cakephp-filter

CakePHP 3.x 过滤插件

安装: 969

依赖: 0

建议: 0

安全: 0

星标: 2

关注者: 0

分支: 2

公开问题: 2

类型:cakephp-plugin

1.0.0 2017-01-25 19:00 UTC

This package is auto-updated.

Last update: 2024-09-10 20:58:34 UTC


README

Software License Total Downloads Latest Stable Version Build Status Coverage Status

安装

您可以使用 composer 将此插件安装到您的 CakePHP 应用程序中。

安装 composer 包的推荐方法是

composer require brenoroosevelt/cakephp-filter

加载插件

将以下内容添加到您的 config/bootstrap.php

Plugin::load('BRFilter');

用法

控制器类

public function index()
{
		$this->loadComponent('BRFilter.Filter');
		
		// add filter and options
		$this->Filter->addFilter([
					'filter_id' => ['field' => 'Posts.id', 'operator'=>'='],
					'filter_title' => ['field' => 'Posts.title', 'operator' => 'LIKE', 'explode' => 'true'],
					'filter_category_id' => ['field'=> 'Posts.category_id', 'operator' => 'IN' ] 
		]);
		
		// get conditions
		$conditions = $this->Filter->getConditions(['session'=>'filter']);
		
		// set url for pagination
    	$this->set('url', $this->Filter->getUrl());
    	
    	// apply conditions to pagination
    	$this->paginate['conditions']	= $conditions;
    	
    	// get pagination 
    	$this->set('posts', $this->paginate($this->Posts));
    	
    	// ...
}

模板视图

您需要在 index.ctp 中添加一个表单,与您的过滤器配置别名相对应。

	echo $this->Form->create();
    
   	// Match with the filter configuration in your controller 
    echo $this->Form->input('filter_id', ['type' => 'text']);
    echo $this->Form->input('filter_title', ['type' => 'text']);
    echo $this->Form->input('filter_category_id', ['options'=>[ /* ... */ ], 'multiple'=>'multiple' ]);
    
	echo $this->Form->button('Filter', ['type' => 'submit']);
	echo $this->Form->end();

过滤器选项

以下选项被支持:

  • field (string) 要用于搜索的字段名称。
  • operator (string) 用于搜索的运算符。
  • explode (boolean) 仅与运算符 LIKEILIKE 一起使用,以分解字符串查询。

运算符

以下选项被支持:

  • =
  • >
  • <
  • >=
  • <=
  • LIKE
  • ILIKE
  • IN

持久化查询(会话)

所有查询字符串都通过会话持久化。请确保已加载 Session 组件。