我在我的 CakePHP 项目上设置了一个简单的搜索引擎,如下所示:
<?php
echo $this->Form->create("Post", array(
"action" => "search",
"id" => "searchForm"
));
echo $this->Form->input("keyword", array(
"label" => "",
"type" => "search",
"placeholder" => "Recherche..."
));
echo $this->Form->end();
?>
这是控制器:
function search() {
$keyword = $this->request->data;
$keyword = $keyword["Post"]["keyword"];
$cond = array("OR" => array(
"Post.title LIKE '%$keyword%'",
"Post.description LIKE '%$keyword%'"
));
$posts = $this->Post->find("all", array("conditions" => $cond));
$this->set(compact("posts", "keyword"));
}
而且效果很好。唯一的问题是当我想对结果进行分页时。我只是添加:
$posts = $this->paginate();
这就是问题所在。当我添加这个时,CakePHP 会给我所有的帖子,而不仅仅是那些与关键字匹配的帖子。
因此,如果您有解决方案,那就太好了:)