[英]CakePHP find query using %like%
我有以下查找我的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
試試這個
$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
)
)
);
你也可以試試這個:
$this->paginate = array(
'limit'=>5,
'order'=>'Note.datetime DESC',
'conditions' => array(
'Note.status'=>1,
'OR' => array(
'Note.title LIKE' => "%$q%",
'Note.content LIKE' => "%$q%"
)
)
);
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."%"
)
)
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.