2017-07-29 68 views
0

我想找到所有從柱誰擁有任何關鍵字匹配列數據的記錄:CakePHP的查詢FIND_IN_SET搜索的關鍵字不工作

如果有逗號列搜索單個搜索關鍵詞分開它工作正常值如下面的代碼:CakePHP的3.4版本

$posts = TableRegistry::get('Posts'); 
       $search_keywords = array_filter(explode(' ', $search_string)); 
       $option = [ 
         'contain' => false, 
         'conditions' => [ 
         "find_in_set('New', Posts.title)", 
         ], 
         'order' => ['Posts.created DESC'] 
        ]; 
     $allpost = $posts->find('all',$option)->toArray(); 

注:我想所有的字符串的話應該列標題,不強制逗號分隔記錄中搜索:

$search_keyword = "New car in new delhi"; 

,所以我想代碼是象下面這樣:提前

$search_keyword = "New car in new delhi"; 
$search_keywords = array_filter(explode(' ', $search_string)); 
       $option = [ 
         'contain' => false, 
         'conditions' => [ 
         "find_in_set({$search_keywords}, `Posts`.title)", 
         ], 
         'order' => ['Posts.created DESC'] 
        ]; 
     $allpost = $posts->find('all',$option)->toArray(); 

十分感謝!

回答

0

我找到了解決辦法:

首先要列全文索引,然後添加以下代碼:

$search_string = "New car in new delhi"; 
$option = [ 
         'contain' => false, 
         'conditions' => [ 
         "MATCH (title) AGAINST ('$search_string')" 
         ], 
         'order' => ['Posts.created DESC'] 
        ]; 
     $allpost = $posts->find('all',$option)->toArray(); 
+0

請,** _從來沒有直接_ **插入(用戶)數據轉換成疑問,這是一個可能的SQL注射漏洞! ** _始終_ **使用安全的'key => value'條件或綁定! ** https://book.cakephp.org/3.0/zh/orm/query-builder.html#sql-injection-prevention** – ndm

+0

謝謝。請告訴我如何在cake語法中編寫這個查詢。 –

+0

請檢查鏈接,有一個示例顯示您的確切情況。 – ndm