我使用下面的代码使用全文索引进行搜索
$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
请指教
提前致谢!
最佳答案
尝试将您的查询重写为:
$query = $this->Posts->find()
->contain(['Categories'])
->where([
"MATCH(Posts.title) AGAINST(:search IN BOOLEAN MODE)"
])
->bind(':search', $search);
确保您安装了最新的 CakePHP 3.x 版本,因为这是不久前添加的。
以下方法可行,但请注意,它会使您的代码容易受到 SQL 注入(inject)攻击:
// 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)"
]);
关于CakePHP3 : How can I do text searching using full-text indexes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34358366/