2013-01-22 101 views
1

我想使用來自cakedc的'between'示例,但無法理解它。cakephp cakedc

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

我有場qca_start在我的桌子,想用戶提供(從,到)兩個值,並搜索來自和之間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 ?'), 

)?;

我不知道如何格式化本作的filterExpr:

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

我想qca_start是介於搜索值一,二。

你能幫忙嗎?

+0

我試圖使用他們的搜索插件一次,但放棄了。我相信那些傢伙的CakePHP思維模式超出了我的想法。不過,我打算回來再試一次....很快! –

回答

0

你應該閱讀documenation @https://github.com/cakedc/search

「表達」類型有用的,如果你想添加條件,將 通過某種方法產生,以及條件字段包含諸如用於「以前的樣品在幾個參數 範圍'。這裏的字段包含 'Article.views BETWEEN? AND?'和Article :: makeRangeCondition 返回兩個值的數組

所以只是在你的方法返回2個值:

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

他們將自動替換這兩個?按照這個順序。從答案

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> 

等待f eedback,看看它是否適用於你;)