【问题标题】:CakePHP: Automatic search on pre-selected dropdown valueCakePHP:自动搜索预先选择的下拉值
【发布时间】:2013-02-16 14:45:39
【问题描述】:

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

我希望 CakePHP 在按该值过滤的数据库中执行搜索,并返回分页结果。按下提交按钮时这很容易,但我想在没有用户交互的情况下在页面加载时执行搜索。

现在,在控制器中,我尝试从下拉列表以外的其他地方获取公社值:

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。当我尝试换页时,它不是帖子,搜索条件将再次设置为只有公社值的值,SQL语句出错。

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

我需要有关如何以另一种方式执行此操作的建议。有可能吗?我怀疑有一个简单的解决方案,但我是 CakePHP 的新手,似乎无法理解。

执行搜索

$conditions = $this->setSearchConditions($this->request->data);
$res = $this->paginate('Ad', array($conditions));
$this->set('res', $res);

//limit is set in public $paginate variable

设置搜索条件

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;
}

【问题讨论】:

  • 您真的应该考虑不要重新发明轮子,而是使用现有且运行良好的 github.com/cakedc/search 插件
  • 嗯,这就是我最初所做的,但它没有产生预期的结果。这就是我摆脱它的原因。话虽如此,在将这个问题通过电子邮件发送给 CakeDC 后,我现在又回到了插件上。他们告诉我为用户设置一个带有 commune_id 的默认数组,然后将它与 passArgs 数组合并。

标签: cakephp search drop-down-menu


【解决方案1】:

我尝试了 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());
}

【讨论】:

    猜你喜欢
    • 2018-12-04
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-13
    • 2015-05-21
    • 1970-01-01
    相关资源
    最近更新 更多