首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >过滤cakedc搜索结果

过滤cakedc搜索结果
EN

Stack Overflow用户
提问于 2014-12-01 11:31:20
回答 1查看 176关注 0票数 0

我在我的应用程序中使用了cakeDC的搜索插件。如果我在我正在搜索的username字段中没有传递值,但是将帐户类型筛选器设置为记录的admin或user,那么如果将筛选设置为all和username字段为空,那么结果为什么是空的,将输出系统中的所有用户,将帐户类型筛选器设置为空用户名搜索字段,以便将帐户类型筛选器设置为2种用户类型。

以下是相关代码(如果需要的话):

控制器

代码语言:javascript
复制
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);

模型

代码语言:javascript
复制
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,
                ));
    }

视图搜索表单

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

发布于 2014-12-15 22:50:48

尝试将‘空’和allowEmpty放在用户名和account_type中,如下所示:

代码语言:javascript
复制
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检查正在执行的查询也是个好主意。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27227478

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档