yii模型搜索时间戳的日期范围

时间:2012-03-06 05:57:01

标签: php yii

可以发布任何使用日期选择器过滤网格视图时间戳(Y-m-d h:m:s)列的方法。 我的模型在下面

   public function search()
{
    $criteria=new CDbCriteria();

                $criteria->condition="time_up BETWEEN UNIX_TIMESTAMP('$this->time_up_from') AND UNIX_TIMESTAMP('$this->time_up_to')";
    $criteria->compare('proc_id',$this->proc_id);
    $criteria->compare('book_id',$this->book_id);
    $criteria->compare('Project_name', $this->Project_name);
    $criteria->compare('isbn_no', $this->isbn_no);
    $criteria->compare('book_title',$this->book_title);
    $criteria->compare('totalpage',$this->totalpage,true);
    $criteria->compare('totaltime',$this->totaltime,true);
    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
        'pagination'=>array(
        'pageSize'=>100
    ),
    ));
    }

对于正常特定条件,其工作条件如下

        $criteria->condition = "  time_up LIKE  '$this->time_up%'";

对于日期范围它不工作我也尝试过 wiki/142/ in yii website但没有用。 请大家帮忙。或者提供一些其他方法来搜索时间戳。

我从高级搜索表单输入

        <div class=" wide form">

   <?php $form=$this->beginWidget('CActiveForm', array(
'action'=>Yii::app()->createUrl($this->route),
'method'=>'get',
         )); ?>
    <div class="row">
    <?php echo "Time UP from"; ?>
 <?php $this->widget('zii.widgets.jui.CJuiDatePicker',
 array(
  'model'=>$model, 
'name'=>'Process[time_up_from]',
       // Model attribute filed which hold user input
      'options'=>array(
    'showAnim'=>'fold',
    'dateFormat'=>'yy-mm-dd',),
    'htmlOptions'=>array(
    'style'=>'height:20px;width:100px',
    'size'=>15,
    //'value'=>date('Y-m-d'),
    /*'onchange'=>"$.fn.yiiGridView.update('books-grid', {data: $(this).serialize()});" */),));?>
   </div>
    <?php echo "Time Up to"; ?>
   <?php $this->widget('zii.widgets.jui.CJuiDatePicker',
    array(
  'model'=>$model, 
    'name'=>'Process[time_up_to]',
       // Model attribute filed which hold user input
      'options'=>array(
    'showAnim'=>'fold',
    'dateFormat'=>'yy-mm-dd',),
    'htmlOptions'=>array(
    'style'=>'height:20px;width:100px',
    'size'=>15,
    //'value'=>date('Y-m-d'),
    /*'onchange'=>"$.fn.yiiGridView.update('books-grid', {data:      $(this).serialize()});"*/  ),));?>
           </div>
         <?php   echo CHtml::submitButton('Search'); ?>

解决问题的答案

我发现答案只是条件条件之前的条件

`if(strlen($this->time_up_from) && strlen($this->time_up_to))
    {
 $criteria->condition="time_up BETWEEN UNIX_TIMESTAMP('$this->time_up_from') AND UNIX_TIMESTAMP('$this->time_up_to')";
   }

现在它的工作正常。 @ bool.dev非常感谢你的建议。感谢很多。

2 个答案:

答案 0 :(得分:3)

试试这个:

public function search(){

   $criteria=new CDbCriteria();

   if(!empty($this->time_up_from) && !empty($this->time_up_to)){
      $criteria->condition="time_up BETWEEN UNIX_TIMESTAMP('$this->time_up_from') AND UNIX_TIMESTAMP('$this->time_up_to')";
   }
   $criteria->compare('proc_id',$this->proc_id);
   $criteria->compare('book_id',$this->book_id);
   $criteria->compare('Project_name', $this->Project_name);
   $criteria->compare('isbn_no', $this->isbn_no);
   $criteria->compare('book_title',$this->book_title);
   $criteria->compare('totalpage',$this->totalpage,true);
   $criteria->compare('totaltime',$this->totaltime,true);
   return new CActiveDataProvider($this, array(
      'criteria'=>$criteria,
      'pagination'=>array(
         'pageSize'=>100
      ),
   ));
}

猜测time_up_totime_up_from是您在模型中声明的虚拟属性,请注意您已正确声明它们,并为它们添加了安全验证器,如下所示:

// in your model
public $time_up_from;
public $time_up_to;

// in the rules of the model
return array(
  // other rules
  // below is the safe rule
  array('proc_id, book_id, Project_name, isbn_no, book_title, totalpage, totaltime, time_up, time_up_from, time_up_to', 'safe', 'on'=>'search'),
);

同样在您的搜索表单中修改日期选择器,正如我在评论中已经提到的那样:

 // remove the 'name'=>'Process[time_up_from]' and use the following line
 'attribute'=>'time_up_from'
 // and remove the 'name'=>'Process[time_up_to]' and use the following line
 'attribute'=>'time_up_to'

编辑:

正如Dcoder在下面的评论中指出的那样,我们应该始终绑定params,以防止sql注入,并可能获得改进的性能,因此修改后的条件可能是:

if(!empty($this->time_up_from) && !empty($this->time_up_to)){
  $criteria->condition="time_up BETWEEN UNIX_TIMESTAMP(:time_up_from) AND UNIX_TIMESTAMP(:time_up_to)";
  $criteria->params[':time_up_from']=$this->time_up_from;
  $criteria->parmas[':time_up_to']=$this->time_up_to;
}

From the guide

答案 1 :(得分:1)

我有一个带数字的字段,我使用..来指定输入字段中的范围。 在模型中,我只是这样做:

    // check for .. as a range selector
    if ( stripos( $this->AGE_IN_DAYS, '..') )
    {
        $range = explode( '..', $this->AGE_IN_DAYS );
        $criteria->compare('AGE_IN_DAYS','>='.$range[0]);
        $criteria->compare('AGE_IN_DAYS','<='.$range[1]);
    }
    else {
        $criteria->compare('AGE_IN_DAYS',$this->AGE_IN_DAYS);
    }
简单而且我的选择对用户来说非常符合逻辑。他会输入0..100来选择0到100的范围。不需要额外的字段。