需要查询某时间段的记录,但是数据库里只有一个时间记录,如果写sql的话,很快的,放到yii里一时竟然没办法...

不过,最后还是解决了,使用了一个第三方的插件

参考http://www.yiiframework.com/wiki/461/adding-a-date-range-search-for-cgridview-the-easy-way/

按照说明写就可以的,只是有一点,分页的时候,会出错,目前还不知道是哪里造成的,不过不影响大局,先给他列几百条记录出来

先看一个图吧,如果是需要的,往下看,如果不是需要的,就当路过捧个场~~

我这里的具体代码如下,当然,是yii 生成的代码,我稍加改动,懒就一个字

<?php

/**
* This is the model class for table "mv_search_count".
*
* The followings are the available columns in table 'mv_search_count':
* @property string $id
* @property string $kid
* @property string $searchtime
*/
class MvSearchCount extends CActiveRecord
{
// 满足搜索之用
public $count_kid;
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'mv_search_count';
} /**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('kid, searchtime', 'required'),
array('kid, searchtime', 'length', 'max'=>10),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('id, kid, searchtime', 'safe', 'on'=>'search'),
);
} /**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'fk_keyword' => array(self::BELONGS_TO, 'MvSearchKeywords', 'kid'),
);
} /**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => '主键',
'kid' => 'keywords主键',
'searchtime' => '搜索时的时间',
);
} /**
* Retrieves a list of models based on the current search/filter conditions.
*
* Typical usecase:
* - Initialize the model fields with values from filter form.
* - Execute this method to get CActiveDataProvider instance which will filter
* models according to data in model fields.
* - Pass data provider to CGridView, CListView or any similar widget.
*
* @return CActiveDataProvider the data provider that can return the models
* based on the search/filter conditions.
*/
public function search()
{
// @todo Please modify the following code to remove attributes that should not be searched. $criteria=new CDbCriteria; /*$criteria->compare('id',$this->id,true);
$criteria->compare('kid',$this->kid,true);*/
// $criteria->compare('searchtime',$this->searchtime,true);
// $criteria->distinct = true;
// $criteria->select = 'kid, count(`kid`) as count_kid';
$criteria->select = array('kid', 'count(`kid`) as count_kid');
// $criteria->join = 'left join mv_search_keywords t2 on(t.kid=t2.kid)';
$criteria->order = 'count_kid desc';
$criteria->group = 'kid';
$criteria->mergeWith($this->dateRangeSearchCriteria('searchtime', $this->searchtime)); return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination'=>array('pageSize'=>1000,),
));
} /**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* @param string $className active record class name.
* @return MvSearchCount the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
} /**
* Model behaviors
*/
public function behaviors()
{
return array(
'dateRangeSearch'=>array(
'class'=>'application.components.behaviors.EDateRangeSearchBehavior',
),
);
} }

我把整个model都粘过来,方便对照查看,另外最上面我定义了一个 public $count_kid  这是因为在yii里,我用到的criteria里有以某个查询结果作为(as)为一个名字来查,这个名字在views里也是需要用到的,所以需要提前定义一下,否则是会出错的,views也一同发一下吧

admin.php

<?php
$data = $model->search();
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'mv-search-count-grid',
'dataProvider'=>$data,
'filter'=>$model,
'columns'=>array(
// 'id',
// 'kid',
array(
'name'=>'kid',
'value'=>'$data->fk_keyword->keyword',
'filter'=>false, // Set the filter to false when date range searching
),
array(
'header'=>'搜索次数',
'value'=>'$data->count_kid',
'filter'=>false,
),
// 'searchtime',
/*array(
'name'=>'searchtime',
'value'=>"Yii::app()->dateFormatter->formatDateTime(\$data->searchtime, 'medium', 'short')",
'filter'=>false, // Set the filter to false when date range searching
),*/
/*array(
'class'=>'CButtonColumn',
),*/
),
)); ?>

最新文章

  1. c++实现蛇形矩阵总结
  2. 点餐系统web版功能需求
  3. Oracle创建表时涉及的参数解析
  4. (原)Struts 相关资源下载
  5. [资源共享]C#+AE构建GIS桌面端应用系统框架-全代码
  6. python自学笔记(一)简单了解python
  7. 05-IOSCore - 单例模式、KVO
  8. Python爬虫入门教程 14-100 All IT eBooks多线程爬取
  9. centos7.4安装npm
  10. MySql修改数据表的基本操作(DDL操作)
  11. cent OS 7查询IP
  12. BZOJ5093图的价值(斯特林数)
  13. A1057. Stack
  14. ThreadPoolExecutor线程池详解
  15. 走进JVM之一 自己编译openjdk源码
  16. [沈航软工教学] 学生项目Coding地址汇总
  17. AngularJS常用Directives实例
  18. react config test env with jest and create-react-app 1
  19. 开启Apache Server Status
  20. Electron build 无法下载 winCodeSign 等资源

热门文章

  1. [转]mysql 的日志的启动与查看
  2. 纯蓝ICON_学习教程
  3. dbm数据库
  4. thinkphp ajax 实例 实现
  5. 习题二:string数组应用
  6. SSS小记
  7. EPZS搜索过程
  8. C# .NET开发Oracle数据库应用程序
  9. AOJ 0121 广度优先搜索
  10. mysql启动停止,一台服务器跑 多个mysql数据库