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

gon*_*nzo 2 cakephp cakephp-3.0

我使用下面的代码来搜索全文索引

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

但我得到了以下错误

SQLSTATE[HY093]: Invalid parameter number: mixed named and positional parameters
Run Code Online (Sandbox Code Playgroud)



提前告知, 谢谢!

Ini*_*res 5

尝试将您的查询重写为:

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

确保安装了最新的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)" 
    ]);
Run Code Online (Sandbox Code Playgroud)