2017-07-26 102 views
0

我在電子商務網站上工作,我需要一些像價格,類型的高級過濾器。與cakephp分頁與高級過濾器一起工作

控制器

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

初始查詢工作正常

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' 

當我點擊ADVANCE過濾器複選框下面的查詢生成被也蠻好這個MySQL查詢包含14記錄下12種產品設置爲ON第一頁,其餘的都是第二頁上

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%')) 

但看到FILTER查詢下一個產品,當我點擊下頁我的查詢則完全改變現在我看到下面的查詢,其中包含所有記錄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' 

回答

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

控制器

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')); 
} 

視圖中使用FORM方法獲取

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