CakePHP3: How can I do text searching using full-text indexes

梦想与她 提交于 2019-12-11 00:53:56

问题


I used the below code to search using full-text indexes

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

But I got the below error

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

Please advise

Thanks in advance!


回答1:


Try rewriting your query as:

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

Make sure you have the latest CakePHP 3.x version installed, as this was added no so long ago.

The following will work, but be warned it makes your code vulnerable to SQL injection attacks:

// 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)" 
    ]);


来源:https://stackoverflow.com/questions/34358366/cakephp3-how-can-i-do-text-searching-using-full-text-indexes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!