我使用以下代码使用全文索引进行搜索
$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
请指教
提前谢谢!
答案 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)"
]);