simtecsystem/cakephp-filter

CakePHP >3.6 的过滤器插件

安装: 12

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 2

类型:cakephp-plugin

1.1.1 2019-01-15 17:33 UTC

This package is auto-updated.

Last update: 2024-09-16 05:53:40 UTC


README

Software License Total Downloads Latest Stable Version Build Status Coverage Status

安装

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

安装 composer 包的推荐方法是

composer require simtecsystem/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 组件。