CakePHP3:如何使用全文索引进行文本搜索

时间:2015-12-18 14:57:32

标签: cakephp cakephp-3.0

我使用以下代码使用全文索引进行搜索

$query = $this->Posts->find()
    ->contain(['Categories' ])
    ->where([
        'MATCH(Posts.title) AGAINST(? IN BOOLEAN MODE)' => $search
]);

但是我得到了以下错误

SQLSTATE[HY093]: Invalid parameter number: mixed named and positional parameters

请指教

提前谢谢!

1 个答案:

答案 0 :(得分:5)

尝试将您的查询重写为:

$query = $this->Posts->find()
    ->contain(['Categories'])
    ->where([
        "MATCH(Posts.title) AGAINST(:search IN BOOLEAN MODE)" 
    ])
    ->bind(':search', $search);

确保安装了最新的CakePHP 3.x版本,因为这是很久以前添加的。

以下内容可行,但请注意,它会使您的代码容易受到SQL注入攻击

// for educational purposes only. Don't use in production environments
$query = $this->Posts->find() 
    ->contain(['Categories'])
    ->where([
        "MATCH(Posts.title) AGAINST('{$search}' IN BOOLEAN MODE)" 
    ]);