使用带有高级过滤器的cakephp分页

时间:2017-07-26 05:30:10

标签: pagination cakephp-2.0

我正在开发电子商务网站,我需要一些高级过滤器,如价格,类型。

控制器

public function index($category) {
$this->set('category',$category);
$this->loadModel("Product");    

$conditions['Product.category'] = $category;    
if(!empty($this->request->data['filter']['materialtype']))
{
foreach($this->request->data['filter']['materialtype'] as $v)
{       
$this->set('v',$v); 
$conditions['OR'][]['Product.materialtype LIKE'] ="%$v%";
}
}
$this->set('agetProduct',$this->paginate($conditions)); 

}

查看 enter image description here

INITIAL QUERY WORKING FINE

1 SELECT `Product`.`id`, `Product`.`category`, `Product`.`materialtype`, `Product`.`occasion`, `Product`.`collections`, `Product`.`stone`, `Product`.`sku`, `Product`.`name`, `Product`.`goldkaratage`, `Product`.`weight`, `Product`.`stoneweight`, `Product`.`diamondcaratage`, `Product`.`diamondcolor`, `Product`.`diamondclarity`, `Product`.`picture1`, `Product`.`picture2`, `Product`.`picture3`, `Product`.`picture4`, `Product`.`picture5`, `Product`.`picture6`, `Product`.`price`, `Product`.`description`, `Product`.`featured`, `Product`.`action` FROM `mookim`.`products` AS `Product` WHERE `Product`.`category` = 'Pendant' LIMIT 12
2   SELECT COUNT(*) AS `count` FROM `mookim`.`products` AS `Product` WHERE `Product`.`category` = 'Pendant'

当我点击预测过滤器后面的查询文件时,这些内容也很精细,这个MYSQL查询包含14条记录,其中第一页上设置了12个产品,第二页上有静止

 1 SELECT `Product`.`id`, `Product`.`category`, `Product`.`materialtype`, `Product`.`occasion`, `Product`.`collections`, `Product`.`stone`, `Product`.`sku`, `Product`.`name`, `Product`.`goldkaratage`, `Product`.`weight`, `Product`.`stoneweight`, `Product`.`diamondcaratage`, `Product`.`diamondcolor`, `Product`.`diamondclarity`, `Product`.`picture1`, `Product`.`picture2`, `Product`.`picture3`, `Product`.`picture4`, `Product`.`picture5`, `Product`.`picture6`, `Product`.`price`, `Product`.`description`, `Product`.`featured`, `Product`.`action` FROM `mookim`.`products` AS `Product` WHERE `Product`.`category` = 'Pendant' AND ((`Product`.`materialtype` LIKE '%Yellow Gold%') OR (`Product`.`materialtype` LIKE '%White Gold%') OR (`Product`.`materialtype` LIKE '%Silver%')) LIMIT 12

2   SELECT COUNT(*) AS `count` FROM `mookim`.`products` AS `Product` WHERE `Product`.`category` = 'Pendant' AND ((`Product`.`materialtype` LIKE '%Yellow Gold%') OR (`Product`.`materialtype` LIKE '%White Gold%') OR (`Product`.`materialtype` LIKE '%Silver%'))

但是当我点击下一页时会看到下一个过滤器查询产品我的查询完全变为现在我看到包含所有SQL表记录的查询

1 SELECT `Product`.`id`, `Product`.`category`, `Product`.`materialtype`, `Product`.`occasion`, `Product`.`collections`, `Product`.`stone`, `Product`.`sku`, `Product`.`name`, `Product`.`goldkaratage`, `Product`.`weight`, `Product`.`stoneweight`, `Product`.`diamondcaratage`, `Product`.`diamondcolor`, `Product`.`diamondclarity`, `Product`.`picture1`, `Product`.`picture2`, `Product`.`picture3`, `Product`.`picture4`, `Product`.`picture5`, `Product`.`picture6`, `Product`.`price`, `Product`.`description`, `Product`.`featured`, `Product`.`action` FROM `mookim`.`products` AS `Product` WHERE `Product`.`category` = 'Pendant' LIMIT 12, 12      5   5   1
2   SELECT COUNT(*) AS `count` FROM `mookim`.`products` AS `Product` WHERE `Product`.`category` = 'Pendant'

1 个答案:

答案 0 :(得分:0)

public function index($category) {
$this->set('category',$category);
$this->loadModel("Product");    
$conditions['Product.category'] = $category;    

CONTROLLER

if(!empty($this->params['url']['data']['filter']['materialtype']))
{   
foreach ($this->params['url']['data']['filter']['materialtype'] as $v){
$conditions['OR'][]['Product.materialtype LIKE'] ="%$v%";
}
}


$this->paginate = array('limit' => 12,'conditions' => $conditions);
$agetProduct = $this->paginate($this->modelClass);
$this->set(compact('agetProduct'));   
}

查看使用表格方法获取

<?php echo $this->Form->create('filter', array('url'=>array('controller'=>'Products', 'action'=>'index',$category),'id'=>'filter','autocomplete'=>'off','type'=>'get'));?>