Reputation: 1
I am trying to search between two prices using cakedc search plugin here is my model code:
public $actsAs = array(
'Search.Searchable'
);
public $filterArgs = array(
'price' => array(
'type' => 'expression',
'method' => 'makeRangeCondition',
'field' => 'Price.views BETWEEN ? AND ?'
)
);
public function makeRangeCondition($data = array()) {
if (strpos($data['price'], ' - ') !== false){
$tmp = explode(' - ', $data['price']);
$tmp[0] = $tmp[0] ;
$tmp[1] = $tmp[1] ;
return $tmp;
} else {
return array($minPrice, $maxPrice) ;
}
}
code for my controller:
public function index() {
$this->Prg->commonProcess();
$cond = $this->Property->parseCriteria($this->passedArgs);
$this->set('properties', $this->paginate('Property', $cond));
}
code for my view:
<?php
echo $this->Form->create();
echo $this->Form->input('minPrice');
echo $this->Form->input('maxPrice');
echo $this->Form->submit(__('Submit'));
echo $this->Form->end();
?>
table sql:
CREATE TABLE IF NOT EXISTS `properties` (
id
varchar(36) NOT NULL,
price
float DEFAULT NULL,
PRIMARY KEY (id
),
UNIQUE KEY ID
(id
)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Upvotes: 0
Views: 280
Reputation: 28
If you use two inputs you should use two arguments in $filterArgs
array.
You can try this code in your Model:
public $filterArgs = array(
'minPrice' => array(
'type' => 'query',
'method' => 'priceRangeMin'
),
'maxPrice' => array(
'type' => 'query',
'method' => 'priceRangeMax'
)
);
public function priceRangeMin( $data = array() ) {
if( !empty( $data['minPrice'] ) ) {
return array('Property.price >= '.$data['minPrice'].'');
}
}
public function priceRangeMax( $data = array() ) {
if( !empty( $data['maxPrice'] ) ) {
return array('Property.price <= '.$data['maxPrice'].'');
}
}
Also use same code above in your controller and view.
Upvotes: 0
Reputation: 101
Instead of 'range' key in filterArgs name it as 'price'. Because plugin checks by the key name and call method only if data[key] is not empty.
Upvotes: 2