CakeDC插件在日期之间搜索

时间:2013-02-26 20:01:15

标签: cakephp date search cakedc

如何使用此插件(CakeDC的一个分支)在日期之间进行搜索?

https://github.com/dereuromark/search

我已阅读过该文档,但我无法做到。即使在我只搜索了一个日期之后,我也没有成功。

public $filterArgs = array(
    'my_date' => array('type' => 'like')
);

在视图中:

echo $this->Form->input('my_date', array('div' => false, 'label' => 'Date', 'dateFormat'     => 'DMY', 'minYear' => date('Y') - 80));

1 个答案:

答案 0 :(得分:3)

有两种方法可以使用CakeDC进行日期搜索,

首先:仅查看特定年份

在模型中

'creationdate'  => array('type' => 'like'),
控制器操作中的

    $this->Prg->commonProcess();

if(array_key_exists('creationdate',$this->passedArgs))
$this->passedArgs['creationdate'] = $this->passedArgs['creationdate']['year'];

并在视野中:

    echo $this->Form->input('creationdate', array(
    'label'             => __('Creation date'),
    'dateFormat'    => 'Y',
    'minYear'           => date('Y') - 100,
    'maxYear'           => date('Y'),
    'empty'             => true
    )
);
第二种方式:寻找一系列日期

模特中的

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

在视图中:请注意我正在使用年份范围的滑块

    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>

希望它有所帮助!