cakephp查询单列上的find_in_set,其中多个搜索关键字不起作用

时间:2017-07-29 10:03:43

标签: mysql cakephp-3.x

我想查找列中与列数据匹配的任何关键字的所有记录:

如果在列中使用逗号分隔值搜索单个搜索关键字,如下面的代码:Cakephp 3.4 version

$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();

非常感谢提前!!!

1 个答案:

答案 0 :(得分: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();