yii 模型搜索与时间戳的日期范围 [英] yii model search with daterange for time stamp

查看:40
本文介绍了yii 模型搜索与时间戳的日期范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 公共函数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);返回新的 CActiveDataProvider($this, array('标准'=>$标准,'分页'=>数组('pageSize'=>100),));}

对于正常的特定条件,它在以下条件下工作

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

对于日期范围它不起作用我也尝试过wiki/142/在 yii 网站 但没有用.请在这方面提供帮助.或提供一些其他方法来搜索时间戳.

我从高级搜索表单输入的内容

 

<?php $form=$this->beginWidget('CActiveForm', array('action'=> Yii::app()->createUrl($this->route),'方法'=>'获取',));?><div class="row"><?php echo "Time UP from";?><?php $this->widget('zii.widgets.jui.CJuiDatePicker',大批('模型'=>$模型,'name'='进程[time_up_from]',//保存用户输入的模型属性字段'选项'=>数组('showAnim'='折叠','dateFormat'=''yy-mm-dd',),'htmlOptions'=>数组('样式'=>'高度:20像素;宽度:100像素','大小'=> 15,//'value'=>date('Y-m-d'),/*'onchange'=>"$.fn.yiiGridView.update('books-grid', {data: $(this).serialize()});"*/),));?>

<?php echo "时间到";?><?php $this->widget('zii.widgets.jui.CJuiDatePicker',大批('模型'=>$模型,'name'='进程[time_up_to]',//保存用户输入的模型属性字段'选项'=>数组('showAnim'='折叠','dateFormat'=''yy-mm-dd',),'htmlOptions'=>数组('样式'=>'高度:20像素;宽度:100像素','大小'=> 15,//'value'=>date('Y-m-d'),/*'onchange'=>"$.fn.yiiGridView.update('books-grid', {data: $(this).serialize()});"*/),));?>

<?php echo CHtml::submitButton('Search');?>

问题的答案

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

`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 非常感谢您的建议.非常感谢.

解决方案

试试这个:

公共函数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);返回新的 CActiveDataProvider($this, array('标准'=>$标准,'分页'=>数组('pageSize'=>100),));}

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

//在您的模型中公共 $time_up_from;公共 $time_up_to;//在模型规则中返回数组(//其他规则//下面是安全规则array('proc_id, book_id, Project_name, isbn_no, book_title, totalpage, totaltime, time_up, time_up_from, time_up_to', 'safe', 'on'=>'search'),);

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

//删除 'name'=>'Process[time_up_from]' 并使用以下行'属性'=>'time_up_from'//并删除 'name'=>'Process[time_up_to]' 并使用以下行'属性'=>'time_up_to'

Dcoder 在下面的评论中指出,我们应该始终绑定参数,以防止 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;}

来自指南

can any post the how to filter a grid view timestamp(Y-m-d h:m:s) column using date picker. my model is below

   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
    ),
    ));
    }

for the normal particular condition its working by below condition

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

for date range its not working i tried also wiki/142/ in yii website but no use. kindly help in this.or give some other methods to darange search for timestamp.

My inputs from advanced search form

        <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'); ?>

ANSWER FOR THE PROBLEM

hi i found the answer its just a if condition before criteria condition

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

now its working fine. @bool.dev thank you very much for your suggestions.thanks alot.

解决方案

Try this:

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
      ),
   ));
}

Guessing that time_up_to and time_up_from are virtual attributes that you have declared in your model, take care that you have declared them properly, and also added the safe validator for them, like this:

// 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'),
);

Also in your search form modify the date pickers, as i already mentioned in the comments:

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

Edit :

As Dcoder pointed out in the comments below, we should always bind params, to prevent sql injection, and possibly get improved performance, hence the modified condition could be:

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

这篇关于yii 模型搜索与时间戳的日期范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆