CakePHP:自动搜索预选下拉列表值

CakePHP:自动搜索预选下拉列表值,cakephp,search,drop-down-menu,Cakephp,Search,Drop Down Menu,我有一个搜索表单,它有一个文本输入框、三个复选框和一个基于数据库中用户数据的下拉列表中预选的默认值。 e、 g.如果用户居住在社区1中,则下拉列表中选择1作为默认值 我希望CakePHP在数据库中执行搜索,并根据该值进行过滤,然后返回分页结果。当按下submit按钮时,这很容易,但我想在页面加载时执行搜索,而不需要用户交互 现在,在控制器中,我尝试从下拉列表以外的其他位置获取commune值: if ($this->request->is('post)) { //Perfor

我有一个搜索表单,它有一个文本输入框、三个复选框和一个基于数据库中用户数据的下拉列表中预选的默认值。 e、 g.如果用户居住在社区1中,则下拉列表中选择1作为默认值

我希望CakePHP在数据库中执行搜索,并根据该值进行过滤,然后返回分页结果。当按下submit按钮时,这很容易,但我想在页面加载时执行搜索,而不需要用户交互

现在,在控制器中,我尝试从下拉列表以外的其他位置获取commune值:

if ($this->request->is('post)) {
    //Perform normal search with the other input fields included.    
} else {
    //Do the filtered search only by commune value, which I get from a function.   
}
问题是,那么分页将不起作用。这是预期的,因为分页使用GET。当我尝试更改页面时,它不是帖子,搜索条件将再次设置为仅commune值的搜索条件,并且我在SQL语句中得到一个错误

如果我上面的解释有点混乱,我很抱歉,但你得原谅我,因为英语不是我的第一语言

我需要建议如何以另一种方式做到这一点。有可能吗?我怀疑有一个简单的解决方案,但我是CakePHP新手,似乎无法理解

执行搜索 设置搜索条件
我尝试了CakeDC搜索插件(见第一个问题中对Marks评论的回答),并通过电子邮件向他们发送了一个类似的问题。解决方案是使用默认搜索条件设置一个数组,并将其与搜索条件合并。以下是控制器代码:

public function view() {
    $this->set('title_for_layout', 'Localtrade Norway');
    $this->set('show_searchbar', true); //Shows searchbar div in view
    $this->log($this->request->data, 'debug');

    //Setting users home commune as default filter when the form is not submitted.
    $default_filter = array(
        'Ad.commune_id' => $this->Auth->user('User.commune_id')
    );

    $this->Prg->commonProcess(); //Search-plugin
    $this->paginate = array(
        'conditions' => array_merge($default_filter, $this->Ad->parseCriteria($this->passedArgs)), //If Ad.commune_id is empty in second array, then the first will be used.
        'fields' => $this->Ad->setFields(),
        'limit' => 3
    );
    $this->set('res', $this->paginate());
}

你真的应该考虑不重新发明轮子,但使用现有的和工作良好的插件,这是我最初做的,但它并没有产生预期的结果。这就是为什么我把它扔掉了。话虽如此,在将此问题通过电子邮件发送给CakeDC后,我现在回到插件。他们告诉我为用户设置一个带有commune_id的默认数组,然后将其与passedArgs数组合并。
private function setSearchConditions($data) {

    $conditions = array();
   // $this->log('Search: DATA', 'debug');
    //$this->log($data, 'debug');

    if ($this->request->is('post)) { //Submit-button is clicked, performing full search
        //$this->log('Dette er en post', 'debug');

        if ($data['Ad']['searchField']) { //Text searchfield is not empty, adding title or description to search criteria
            $this->log('Søkefeltet er ikke tomt', 'debug');
            $str_search = '%' . $data['Ad']['searchField'] . '%';
            $conditions[] = array(
                'OR' => array(
                    'Ad.title LIKE' => $str_search,
                    'Ad.description LIKE' => $str_search
                )
            );
        }//if

        if ($data['Ad']['commune_id']) { // Commune dropdown is not empty, adding   commune_id to search criteria
            $conditions[] = array(
                'Ad.commune_id' => $data['Ad']['commune_id']
            );
        }//if

        if ($data['Ad']['type_id']) { // Type checkboxes are not empty, adding type_id to search criteria
            $orArray = array();
            foreach ($data['Ad']['type_id'] as $type) {
                $orArray[] = array('Ad.type_id' => $type);
            }
            $conditions[] = array(
                'OR' => $orArray
            );
        }//if
    } else {
       $conditions[] = array(
         'Ad.commune_id' => $this->getDefaultCommune(); 
       ):
    }

    return $conditions;
}
public function view() {
    $this->set('title_for_layout', 'Localtrade Norway');
    $this->set('show_searchbar', true); //Shows searchbar div in view
    $this->log($this->request->data, 'debug');

    //Setting users home commune as default filter when the form is not submitted.
    $default_filter = array(
        'Ad.commune_id' => $this->Auth->user('User.commune_id')
    );

    $this->Prg->commonProcess(); //Search-plugin
    $this->paginate = array(
        'conditions' => array_merge($default_filter, $this->Ad->parseCriteria($this->passedArgs)), //If Ad.commune_id is empty in second array, then the first will be used.
        'fields' => $this->Ad->setFields(),
        'limit' => 3
    );
    $this->set('res', $this->paginate());
}