Carlos Garcia
Carlos Garcia

Reputation: 359

cakephp cakedc between

I'd like to use the 'between' example from cakedc, but just can't make sense out of it.

'range' => array('type' => 'expression', 'method' => 'makeRangeCondition', 'field' => 'Article.views BETWEEN ? AND ?'),

I have field qca_start in my table and want user to provide two values (from, to) and search for qca_start between from and to.

My controller: (I've used other simpler searches without problem. (employee_id works just fine here)

public $presetVars = array(
    array('field' => 'employee_id', 'type' => 'value'),
    array('field' => 'qca_start', 'type' => 'value') // not sure what type to use here for between search.

}; The field on my table is qca_start, not user how would i name the presetVar for this?

On my model

public $filterArgs = array(
array('name' => 'employee_id', 'type' => 'value'),
    'range' => array('type' => 'expression', 'method' => 'makeRangeCondition', 'field' => 'Article.views BETWEEN ? AND ?'),

);

I don't know how to format this for filterArgs:

'range' => array('type' => 'expression', 'method' => 'makeRangeCondition', 'field' => 'Article.views BETWEEN ? AND ?'),

I want qca_start to be between search values One and Two.

Can you help?

Upvotes: 1

Views: 766

Answers (2)

mohamed mellouki
mohamed mellouki

Reputation: 613

a copy/paste from the answer i gave @ CakeDC Plugin Search Between Dates

in model :

        'creationDateBetween'       => array(
            'type'      => 'expression',
            'method'    => 'CreationDateRangeCondition',
            'field'     => 'MODEL.creationdate BETWEEN ? AND ?',
        ),

public function CreationDateRangeCondition($data = array()){
    if(strpos($data['creationDateBetween'], ' - ') !== false){
        $tmp = explode(' - ', $data['creationDateBetween']);
        $tmp[0] = $tmp[0]."-01-01";
        $tmp[1] = $tmp[1]."-12-31";
        return $tmp;
    }else{
        return array($data['creationDateBetween']."-01-01", $data['creationDateBetween']."-12-31");
    }
}

in view : note that i'm using a slider for year range

    echo $this->Form->input('creationDateBetween', 
    array(
        'label'     => __('Creation date between X and Y'),
        'div'       => false,
        'style' => 'border: 0; color: #49AFCD; font-weight: bold;'
    )
);

?><div id="creationDateBetweenSlider" style="padding:0;"></div><?php

<script>
$(function() {


    var     creationDateBetweenSlider = $( "#creationDateBetweenSlider" ),
        institutionCreationDateBetween  = $( "#MODELCreationDateBetween" ), 

lock = 0;

creationDateBetweenSlider.slider({
                range: true,
                min: 1900,
                max: 2050,
                values: [ 2000, 2013 ],
                slide: function( event, ui ) {
                    MODELCreationDateBetween.val(  ui.values[ 0 ] + " - " + ui.values[ 1 ] );
                }
            });
        if(lock != 0) MODELCreationDateBetween.val( creationDateBetweenSlider.slider( "values", 0 ) + " - " + creationDateBetweenSlider.slider( "values", 1 ) );

lock = 1;

    });
</script>

waiting for feedback to see if it works for you ;)

Upvotes: 1

mark
mark

Reputation: 21743

You should read the documenation @ https://github.com/cakedc/search

'expression' type useful if you want to add condition that will generate by some method, and condition field contain several parameter like in previous sample used for 'range'. Field here contains 'Article.views BETWEEN ? AND ?' and Article::makeRangeCondition returns array of two values.

So just return 2 values in your method:

public function makeRangeCondition() {
    ...
    return array($from, $to);
}

They will automatically replace the two ? in this order then.

Upvotes: 0

Related Questions