我的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
的参数来对标题和内容进行查询。
例如,如果我有以下内容:
Title: Lorem ipsum
Content: 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
答案 0 :(得分:6)
试试这个
$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
)
)
);
答案 1 :(得分:6)
你也可以试试这个:
$this->paginate = array(
'limit'=>5,
'order'=>'Note.datetime DESC',
'conditions' => array(
'Note.status'=>1,
'OR' => array(
'Note.title LIKE' => "%$q%",
'Note.content LIKE' => "%$q%"
)
)
);
答案 2 :(得分:1)
LIKE在这里不会帮到你。您的标题为Lorem ipsum
,内容为Lorem ipsum dolare
。您正在搜索lorem dolare
,它不会通过LIKE运算符找到,因为它不是这些列中的任何一个子串。您需要考虑使用FULLTEXT,否则您需要使用类似Sphinx的内容。如果您正在尝试找出要实施的解决方案,请查看this answer here。
答案 3 :(得分:0)
Please Try this:
$this->paginate = array(
'limit'=>5,
'order'=>'Note.datetime DESC',
'conditions' => array(
'Note.status'=>1,
'OR' => array(
'Note.title LIKE' => "%".$q."%",
'Note.content LIKE' => "%".$q."%"
)
)
);