2013-11-28 17 views
6

找到查詢我有我的CakePHP應用中的以下查找查詢:CakePHP的使用像%%

$this->paginate = array(
'limit'=>5, 
'order'=>'Note.datetime DESC', 
'conditions' => array(
    'Note.status'=>1, 
    'OR' => array(
     'Note.title LIKE' => '%'. $q . '%', 
     'Note.content LIKE' => '%'. $q . '%' 
     ) 
    ) 
); 

這需要一個名爲$q做這樣兩個標題和內容查詢參數。

因此,舉例來說,如果我有以下幾點:

Title: Lorem ipsum Content: Lorem ipsum dolare

和搜索'lorem'

這將精細找到它。但是,如果我搜索'lorem dolare'它不會找到它。

我該怎麼做?

注:我不希望使用任何插件等

編輯:如果我使用FULLTEXT將這項工作?

$this->paginate = array(
'limit'=>5, 
'order'=>'Note.datetime DESC', 
'conditions' => array(
    'Note.status'=>1, 
    'OR' => array(
     'MATCH(Note.title) AGAINST('.$q.' IN BOOLEAN MODE)', 
     'MATCH(Note.content) AGAINST('.$q.' IN BOOLEAN MODE)' 
     ) 
    ) 
); 

編輯2:

收到此錯誤在嘗試上述:

Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'dolor IN BOOLEAN MODE)) OR (MATCH(`Note`.`content`) AGAINST(lorem dolor IN BOOLE' at line 1 

SQL Query: SELECT `Note`.`id`, `Note`.`title`, `Note`.`excerpt`, `Note`.`content`, `Note`.`datetime`, `Note`.`user_id`, `Note`.`slug`, `Note`.`status`, `Note`.`topic_id`, `User`.`id`, `User`.`email`, `User`.`firstname`, `User`.`lastname`, `User`.`password`, `User`.`status`, `Topic`.`id`, `Topic`.`title`, `Topic`.`slug` FROM `db52704_driz2013`.`notes` AS `Note` LEFT JOIN `db52704_driz2013`.`users` AS `User` ON (`Note`.`user_id` = `User`.`id`) LEFT JOIN `db52704_driz2013`.`topics` AS `Topic` ON (`Note`.`topic_id` = `Topic`.`id`) WHERE `Note`.`status` = 1 AND ((MATCH(`Note`.`title`) AGAINST(lorem dolor IN BOOLEAN MODE)) OR (MATCH(`Note`.`content`) AGAINST(lorem dolor IN BOOLEAN MODE))) ORDER BY `Note`.`datetime` DESC LIMIT 5 
+0

嘗試@弗雷德-II-除去第一''% –

+0

仍然沒有找到它。 – Cameron

+0

我正在考慮Wayne在FULLTEXT搜索下發布的內容。看看這個問題關於SO http://stackoverflow.com/q/17294924/1415724可能會有所幫助。 –

回答

6

試試這個

$this->paginate = array(
    'limit'=>5, 
    'order'=>'Note.datetime DESC', 
    'conditions' => array(
    'Note.status'=>1, 
    'OR' => array(
     "MATCH(Note.title) AGAINST('".$q."' IN BOOLEAN MODE)", 
     "MATCH(Note.content) AGAINST('".$q."' IN BOOLEAN MODE)" 
    ) 
) 

);

換句話說,則會以引號

搜索條件

編輯NDM的建議是有道理的

$this->paginate = array(
    'limit'=>5, 
    'order'=>'Note.datetime DESC', 
    'conditions' => array(
    'Note.status'=>1, 
    'OR' => array(
     'MATCH(Note.title) AGAINST(? IN BOOLEAN MODE)' => $q, 
     'MATCH(Note.content) AGAINST(? IN BOOLEAN MODE)' => $q 
    ) 
) 

);

+0

繁榮:)完美的作品。謝謝 – Cameron

+2

這也是一個非常好的SQL注入漏洞! ;)請使用適當的[**數組語法**](http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#complex-find-conditions)代替:'「MATCH (Post.title)反對(?在布爾模式)「=> $ q' @Cameron – ndm

+0

@ndm你能準確地發表你認爲整個事情應該是什麼樣子嗎?謝謝 – Cameron

1

LIKE不會幫助你在這裏。您的標題是Lorem ipsum,您的內容是Lorem ipsum dolare。您正在搜索lorem dolare,這不會通過LIKE運算符找到,因爲它不是這些列中的任何一個的子字符串。你需要考慮使用FULLTEXT,否則你需要使用類似Sphinx的東西。如果你想弄清楚實施什麼解決方案,請看this answer here

+0

然後你會有什麼建議? – Cameron

+0

@Cameron更新了我的答案。 –

+0

你能分享一個例子嗎?我發現這:http://stackoverflow.com/questions/10827444/cakephp-fulltext-search-mysql-with-rating什麼我想要什麼好? – Cameron

4

你也可以試試這個:

$this->paginate = array(
    'limit'=>5, 
    'order'=>'Note.datetime DESC', 
    'conditions' => array(
     'Note.status'=>1, 
     'OR' => array(
      'Note.title LIKE' => "%$q%", 
      'Note.content LIKE' => "%$q%" 
     ) 
    ) 
); 
0
Please Try this: 

$this->paginate = array(
    'limit'=>5, 
    'order'=>'Note.datetime DESC', 
    'conditions' => array(
     'Note.status'=>1, 
     'OR' => array(
      'Note.title LIKE' => "%".$q."%", 
      'Note.content LIKE' => "%".$q."%" 
     ) 
    ) 
);