cakephp - 使用多字搜索字符串构建查询,搜索多个字段

时间:2013-03-15 14:44:20

标签: php mysql cakephp

我有一个模型'Mix',它有字段(我想要搜索)'name''description'和'tag_words',以及一个布尔字段'published'。如果“已发布”字段设置为1

,用户应该能够输入多字搜索字词并获取任何可搜索字段中出现的任何字词的结果

到目前为止,谷歌的帮助和阅读在这里我得到了:

if ($this->request->is('post')) {
    $conditions = array();
    $search_terms = explode(' ', $this->request->data['Mix']['search']);
    foreach($search_terms as $search_term){
        $conditions[] = array('Mix.name Like' =>'%'.$search_term.'%');
        $conditions[] = array('Mix.description Like' =>'%'.$search_term.'%');
        $conditions[] = array('Mix.tag_words Like' =>'%'.$search_term.'%');
    }
    $searchResults = $this->paginate('Mix', array('conditions' => array('Mix.published'=>1, 'AND'=>array('OR' => $conditions))));
}

但是因为它只返回大量错误,我猜它完全错了。我很困惑,正确的语法是什么?

1 个答案:

答案 0 :(得分:1)

您不使用AND索引,已暗示: -

$searchResults = $this->paginate('Mix', array('conditions' => array('Mix.published'=>1, 'OR' => $conditions)));