php CakePHP 使用 %like% 查找查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20274409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CakePHP find query using %like%
提问by Cameron
I have the following find query for my CakePHP app:
我的 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 . '%'
)
)
);
Which takes a parameter called $qto do like query on both title and content.
它需要一个参数来调用$q对标题和内容的查询。
So for example if I have the following:
例如,如果我有以下内容:
Title: Lorem ipsumContent: Lorem ipsum dolare
Title: Lorem ipsumContent: Lorem ipsum dolare
And search for 'lorem'
并搜索 'lorem'
It would find it fine. But if I search for 'lorem dolare'it won't find it.
它会发现它很好。但如果我搜索'lorem dolare'它不会找到它。
How do I do that?
我怎么做?
Note: I don't want to use any plugins etc.
注意:我不想使用任何插件等。
EDIT: If I use FULLTEXT would this work?
编辑:如果我使用 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)'
)
)
);
EDIT 2:
编辑2:
Getting this error trying the above:
尝试上述操作时出现此错误:
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
采纳答案by Guillermo Mansilla
Try this
尝试这个
$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)"
)
)
);
In other words, enclose the search criteria with quotes
换句话说,用引号将搜索条件括起来
EDIT ndm'ssuggestion makes sense
编辑 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
)
)
);
回答by monsur.hoq
You can also 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%"
)
)
);
回答by Wayne Whitty
LIKE won't help you here. Your title is Lorem ipsumand your content is Lorem ipsum dolare. You're searching for lorem dolare, which won't be found via the LIKE operator as it is not a substring inside either of those columns. You'll need to look into using FULLTEXT or you'll need to use something like Sphinx. Take a look at this answer hereif you're trying to figure out what solution to implement.
LIKE在这里帮不了你。您的标题是Lorem ipsum,您的内容是Lorem ipsum dolare。您正在搜索lorem dolare,它不会通过 LIKE 运算符找到,因为它不是这些列中的任何一个内的子字符串。您需要考虑使用 FULLTEXT 或者您需要使用类似Sphinx 的东西。如果您想找出要实施的解决方案,请在此处查看此答案。
回答by Milap Jethwa
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."%"
)
)
);

