cakephp cakedc之间

时间:2013-01-22 23:03:50

标签: cakephp cakedc

我想在'之间使用'来自cakedc的例子,但是不能理解它。

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

我在表格中有字段qca_start,希望用户提供两个值(from,to),并在from和to之间搜索qca_start。

我的控制器: (我已经毫无问题地使用了其他更简单的搜索。(employee_id在这里工作得很好)

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.

}; 我桌上的字段是qca_start,而不是用户如何为此命名presetVar?

在我的模特上

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

);

我不知道如何为filterArgs格式化这个:

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

我希望qca_start位于搜索值One和Two之间。

你能帮忙吗?

2 个答案:

答案 0 :(得分:1)

从我给@ CakeDC Plugin Search Between Dates

的答案中复制/粘贴 模特中的

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

等待反馈,看看它是否适合你;)

答案 1 :(得分:0)

您应该阅读文档@ https://github.com/cakedc/search

  如果你想添加条件,那么

'expression'类型很有用   通过某种方法生成,条件字段包含多个参数   就像之前用于'范围'的样本一样。这里的字段包含   'Article.views BETWEEN?和?'和 Article :: makeRangeCondition   返回两个值的数组

所以只需在方法中返回2个值:

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

他们会自动更换两个吗?按此顺序。