2013-03-15 103 views
0

我有一個模型'混合',其中有字段(我希望可以搜索)'name''description'和'tag_words' ,以及一個布爾字段'published'。用戶應該能夠輸入一個多字搜索字詞並獲得任何出現在任何可搜索字段中的字詞,只要「已發佈」字段設置爲1cakephp - 用多字搜索字符串建立一個查詢,搜索多個字段

到目前爲止,藉助Google和在這裏閱讀我周圍已經有了:

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

但由於它沒有返回值,但錯誤的負載我想這是完全錯誤的。我很困惑,什麼是正確的語法?

+0

你在哪裏上面的代碼(模型或控制器)寫了什麼樣的錯誤你得到了什麼? – sandip 2013-03-15 14:55:28

+0

我把代碼放在控制器中,我在下面的答案的評論中發佈了錯誤。感謝您的幫助 – 2013-03-15 15:36:05

回答

1

您不使用AND指數,它已經暗示: -

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

嘿,謝謝你的迴應。但是,當我嘗試我得到的錯誤:SQL查詢:SELECT'Mix'.'id','Mix'.'created_by','Mix'.'name','Mix'.'description','Mix ''''出版','混合'''臨時','混合''''圖像','混合'''bgcolor','混合''''''''','混合''列','混合'。 'rows','Mix'.'date_added','Mix'.'tile' FROM'homestartpage'.'mixes' AS'Mix' WHERE' conditions' IN(1,Array)LIMIT 20 – 2013-03-15 15:33:04

+0

oops,sorry,the error是:未找到列:1054未知列'條件'在'where子句'那麼有我發佈的查詢 – 2013-03-15 15:35:28

+0

以上啊,好吧,我是愚蠢的。我從別處複製我的代碼,並沒有意識到我需要將分頁添加到控制器。現在排序了。對不起:( – 2013-03-15 17:05:55