我在我的应用程序中使用 cakeDC 的搜索插件。 如果我在正在搜索的用户名字段中不传递任何值,但将帐户类型过滤器设置为管理员或用户,似乎无法弄清楚为什么结果为空 对于记录,将过滤器设置为用户名字段为空的 all 将输出系统中的所有用户尝试复制将帐户类型筛选器设置为全部的搜索行为,用户名搜索字段为空,以将帐户类型筛选器设置为 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>
尝试在用户名中输入"空"和允许空,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 检查正在执行的查询。