0
votes

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;

2
thanks Chris I've tried searching by "like" without any problems using this code {public $actsAs = array( 'Search.Searchable' ); public $filterArgs = array( 'price' => array( 'type' => 'like', 'field' => 'price' ) );}quintin nel
I'm afraid I can't help you with this. But if you add more fitting tags you will get more user attention.Chris

2 Answers

2
votes

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.

0
votes

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.