gpt4 book ai didi

php - CakePHP 使用 %like% 查找查询

转载 作者:可可西里 更新时间:2023-11-01 06:35:48 25 4
gpt4 key购买 nike

我的 CakePHP 应用程序有以下查找查询:

$this->paginate = array(
'limit'=>5,
'order'=>'Note.datetime DESC',
'conditions' => array(
'Note.status'=>1,
'OR' => array(
'Note.title LIKE' => '%'. $q . '%',
'Note.content LIKE' => '%'. $q . '%'
)
)
);

它采用名为 $q 的参数来对标题和内容进行类似查询。

例如,如果我有以下内容:

标题:Lorem ipsum内容:Lorem ipsum dolare

然后搜索'lorem'

它会发现它很好。但是,如果我搜索 'lorem dolare',它不会找到它。

我该怎么做?

注意:我不想使用任何插件等。

编辑:如果我使用 FULLTEXT 这行得通吗?

$this->paginate = array(
'limit'=>5,
'order'=>'Note.datetime DESC',
'conditions' => array(
'Note.status'=>1,
'OR' => array(
'MATCH(Note.title) AGAINST('.$q.' IN BOOLEAN MODE)',
'MATCH(Note.content) AGAINST('.$q.' IN BOOLEAN MODE)'
)
)
);

编辑 2:

尝试上述操作时出现此错误:

 Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'dolor IN BOOLEAN MODE)) OR (MATCH(`Note`.`content`) AGAINST(lorem dolor IN BOOLE' at line 1

SQL Query: SELECT `Note`.`id`, `Note`.`title`, `Note`.`excerpt`, `Note`.`content`, `Note`.`datetime`, `Note`.`user_id`, `Note`.`slug`, `Note`.`status`, `Note`.`topic_id`, `User`.`id`, `User`.`email`, `User`.`firstname`, `User`.`lastname`, `User`.`password`, `User`.`status`, `Topic`.`id`, `Topic`.`title`, `Topic`.`slug` FROM `db52704_driz2013`.`notes` AS `Note` LEFT JOIN `db52704_driz2013`.`users` AS `User` ON (`Note`.`user_id` = `User`.`id`) LEFT JOIN `db52704_driz2013`.`topics` AS `Topic` ON (`Note`.`topic_id` = `Topic`.`id`) WHERE `Note`.`status` = 1 AND ((MATCH(`Note`.`title`) AGAINST(lorem dolor IN BOOLEAN MODE)) OR (MATCH(`Note`.`content`) AGAINST(lorem dolor IN BOOLEAN MODE))) ORDER BY `Note`.`datetime` DESC LIMIT 5

最佳答案

试试这个

$this->paginate = array(
'limit'=>5,
'order'=>'Note.datetime DESC',
'conditions' => array(
'Note.status'=>1,
'OR' => array(
"MATCH(Note.title) AGAINST('".$q."' IN BOOLEAN MODE)",
"MATCH(Note.content) AGAINST('".$q."' IN BOOLEAN MODE)"
)
)
);

换句话说,用引号将搜索条件括起来

编辑 ndm 的建议是有道理的

$this->paginate = array(
'limit'=>5,
'order'=>'Note.datetime DESC',
'conditions' => array(
'Note.status'=>1,
'OR' => array(
'MATCH(Note.title) AGAINST(? IN BOOLEAN MODE)' => $q,
'MATCH(Note.content) AGAINST(? IN BOOLEAN MODE)' => $q
)
)
);

关于php - CakePHP 使用 %like% 查找查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20274409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com