我在我的应用程序中使用了cakeDC的搜索插件。如果我在我正在搜索的username字段中没有传递值,但是将帐户类型筛选器设置为记录的admin或user,那么如果将筛选设置为all和username字段为空,那么结果为什么是空的,将输出系统中的所有用户,将帐户类型筛选器设置为空用户名搜索字段,以便将帐户类型筛选器设置为2种用户类型。
以下是相关代码(如果需要的话):
控制器
public $components = array('Paginator', 'Session','Search.Prg');
public $presetVars = array(
array('field' => 'username', 'type' => 'value'),
array('field' => 'account_type', 'type' => 'value'));
public function admin_index() {
$this->Prg->commonProcess();
$this->paginate = array(
'conditions' => $this->User->parseCriteria($this->passedArgs));
$this->set('users', $this->Paginator->paginate(),'session',$this->Session);模型
public $actsAs = array('Search.Searchable');
public $filterArgs = array( array('name' => 'username', 'type' => 'query', 'method' => 'filterName'),
array('name' => 'account_type', 'type' => 'value'),
);
public function filterName($data, $field = null) {
$nameField = '%' . $data['username'] . '%';
return array(
'OR' => array(
$this->alias . '.username LIKE' => $nameField,
));
}视图搜索表单
<div><?php echo $this->Form->create('User', array(
'novalidate' =>true,'url' => array_merge(array('action' => 'index','admin'=> true), $this->params['pass'])
)); ?>
<?php
$account_types = array(
'' => __('All', true),
0 => __('admin', true),
1 => __('user', true),);
echo $this->Form->input('username', array('div' => false, 'empty' => true)); // empty creates blank option.
echo $this->Form->input('account_type', array('label' => 'account_type', 'options' => $account_types));
echo $this->Form->submit(__('Search', true), array('div' => false));
echo $this->Form->end();
?></div>发布于 2014-12-15 22:50:48
尝试将‘空’和allowEmpty放在用户名和account_type中,如下所示:
public $filterArgs = array( array('name' => 'username', 'type' => 'query', 'method' => 'filterName', 'empty'=>true, 'allowEmpty'=>true),
array('name' => 'account_type', 'type' => 'value','empty'=>true, 'allowEmpty'=>true),
);我从CakeDC/search那里也有过这样的行为,这两个吐露救了我,希望它也能为你服务。
使用DebugKit.Toolbar检查正在执行的查询也是个好主意。
https://stackoverflow.com/questions/27227478
复制相似问题