用CakePHP对搜索结果进行分页
我在CakePHP项目上设置了一个简单的搜索引擎,如下所示:用CakePHP对搜索结果进行分页,cakephp,pagination,Cakephp,Pagination,我在CakePHP项目上设置了一个简单的搜索引擎,如下所示: <?php echo $this->Form->create("Post", array( "action" => "search", "id" => "searchForm" )); echo $this->Form->input("keyword", array( "label" => "",
<?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();
?>
而且效果很好。唯一的问题是我想在什么时候给结果分页。我只想补充一点:
$posts = $this->paginate();
这就是问题所在。当我添加这个时,CakePHP会给我所有的帖子,而不仅仅是那些与关键字匹配的帖子
因此,如果你想找到一个解决方案,那就太好了:)根据CakePHP的书,你应该能够做到
$this->paginate('Post', array(
'OR' => array(
'Post.title LIKE' => "%$keyword%",
'Post.description LIKE' => "%$keyword%"
)
));
或者您可以这样做(从cakephp站点)
资料来源:
我相信Paginate会自己查找数据。您先前调用的
find
对paginate
没有影响
试试这个:
$this->paginate = array( 'conditions' => $cond, ));
您可以使用会话来存储条件
首先,当您提交表单时,您将条件存储到会话中
然后(分页)您可以从会话中读取条件
example i want to search the products:
Products/search.ctp
<?php
echo $this->Form->create('Product');
echo $this->Form->input('keyword');
echo $this->Form->end(__('Search'));
?>
ProductsController.php
<?php
class ProductsController extends AppController{
public function search() {
if ($this->request->is('post')) {
$keyword = $this->request->data['Product']['keyword'];
$this->paginate = array(
'fields' => array('Product.name', 'Product.price', 'Product.created'),
'order' => array('Product.created' => 'DESC', 'Product.price' => 'DESC'),
'limit' => 30,
'conditions' => array('Product.name LIKE' => '%' . $keyword . '%')
);
// store array $this->paginate into Session
$this->Session->write('paginate', $this->paginate);
}
$this->paginate = $this->Session->read('paginate');
$this->set('products', $this->paginate('Product'));
}
}
示例我想搜索产品:
产品/搜索.ctp
ProductsController.php
你看过cake dc搜索插件了吗?这一切都是以一种干净利落的方式完成的——你可以省下一大堆重新发明轮子的麻烦。是的,这是我的第一个想法,但是,我不知道,它就是不想起作用。或者当我设置它时,我做了一些错误的事情……很可能是后者——因为它对我们大多数人来说都是完美的:)你的第一个解决方案就是我首先尝试的。但是,当我这样做时,我在搜索时没有结果:/噢!现在好像有用了。唯一剩下的问题是,它似乎只在Post.description
中查找关键字,这对我来说很奇怪。如果我搜索一个只在标题中而不在描述中的单词,它将找不到它。它产生了什么样的SQL,它看起来正确吗?它是否拾取了OR?使用或
似乎没有问题,但它似乎没有转换Post.title:
选择Post
id
,Post
标题
。。。其中(Post
title
像“%$keyword%”或(Post
像“%test%”)按Post
创建的订单desc LIMIT 16`Man,这不是我的日子。忘记了标题周围的双引号。再次编辑我的答案:-)
$this->paginate = array( 'conditions' => $cond, ));
example i want to search the products:
Products/search.ctp
<?php
echo $this->Form->create('Product');
echo $this->Form->input('keyword');
echo $this->Form->end(__('Search'));
?>
ProductsController.php
<?php
class ProductsController extends AppController{
public function search() {
if ($this->request->is('post')) {
$keyword = $this->request->data['Product']['keyword'];
$this->paginate = array(
'fields' => array('Product.name', 'Product.price', 'Product.created'),
'order' => array('Product.created' => 'DESC', 'Product.price' => 'DESC'),
'limit' => 30,
'conditions' => array('Product.name LIKE' => '%' . $keyword . '%')
);
// store array $this->paginate into Session
$this->Session->write('paginate', $this->paginate);
}
$this->paginate = $this->Session->read('paginate');
$this->set('products', $this->paginate('Product'));
}
}