需要查询某时间段的记录,但是数据库里只有一个时间记录,如果写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',
),*/
),
)); ?>

yii criteria select column as 与 时间段查询的更多相关文章

  1. Yii数据库操作增删改查-[增加\查询\更新\删除 AR模式]

    在Yii的开发中常常需要去使用Yii的增删改查方法,这些方法又可以多次变化和组合,带来全方位的实现对数据库的处理,下面对这些方法做一些简单的整理和梳理,有遗漏或是BUG,敬请指出.灰常感谢!!! 一. ...

  2. jeecg 扩展封装查询条件 时间段查询

    使用jeecg框架开发的小伙伴们知道,添加查询条件,通常是我们加一个配置(query="true")就可以将该字段设置为查询条件.简单方便.但是这样的配置查询条件仅适用于输入框输入 ...

  3. (后端)sql server 按时间段查询

    百度的资料,保存下来: 在写按时间段查询的sql语句的时候 一般我们会这么写查询条件: where date>='2010-01-01' and date<='2010-10-1'. 但是 ...

  4. select * 比select column快很多奇怪案例分析

    遇到MYSQL傻傻的地方,下面给个案例,大家感受下: 注意以下两个sql只有select *和select g.id区别. SQL1:SELECT g.idFROM table1 gINNER JOI ...

  5. bootstrap-table前端实现多条件时间段查询数据

    实现思路:通过正则匹配到字段是否符合条件,时间段转换为时间戳比对. 这是大体的效果图: 页面的html代码 <div class="content-head mgb10"&g ...

  6. 【MYSQL】update/delete/select语句中的子查询

    update或delete语句里含有子查询时,子查询里的表不能在update或是delete语句中,如含有运行时会报错:但select语句里含有子查询时,子查询里的表可以在select语句中. 如:把 ...

  7. mysql select column default value if is null

    mysql select column default value if is null SELECT `w`.`city` AS `city`, `w`.`city_en` AS `city_en` ...

  8. SQL Fundamentals: 子查询 || WHERE,HAVING,FROM,SELECT子句中使用子查询,WITH子句

    SQL Fundamentals || Oracle SQL语言 子查询(基础) 1.认识子查询 2.WHERE子句中使用子查询 3.在HAVING子句中使用子查询 4.在FROM子句中使用子查询 5 ...

  9. EFCore扩展Select方法(根据实体定制查询语句)

    EFCore扩展Select方法(根据实体定制查询语句)  通常用操作数据库的时候查询返回的字段是跟 我们的定义的实体是不一致的,所以往往针对UI或者接口层创建大量的Model, 而且需要手动对应字段 ...

随机推荐

  1. Skew Join与Left Semi Join相关

    Skew Join 真实数据中数据倾斜是一定的, hadoop 中默认是使用 hive.exec.reducers.bytes.per.reducer = 1000000000 也就是每个节点的red ...

  2. When Colon Scripting is comming(JavaScript语法扩充)

    当冒号脚本来临-- JavaScript语法扩充 连续好几夜的不能安眠,得出结论就是,未来语言未来的编译器应该是支持语法定制规则和语法扩展的,这样使得编程语言不断进化以更利于人类使用!of cours ...

  3. openstack nova数据库计算结点IP地址

    最近遇到一个问题就是在控制结点上查找nova数据库中 select * from compute_nodes\G;中出现IP地址一直是127.0.0.1不是计算结点的IP,就算修改成计算结点的IP,也 ...

  4. N3292系列资料之RTC介绍

    N3292系列资料之RTC介绍 1 RTC特性 Ø 拥有时间计数器(秒,分,时)和日历计数器,用来计算时间 Ø 绝对定时功能(秒,分,时,日,月,年) Ø 相对定时功能 Ø 支持12小时/24小时模式 ...

  5. 数据类型和Json格式[转]

    1. 前几天,我才知道有一种简化的数据交换格式,叫做yaml. 我翻了一遍它的文档,看懂的地方不多,但是有一句话令我茅塞顿开. 它说,从结构上看,所有的数据(data)最终都可以分解成三种类型: 第一 ...

  6. dede 留言板访问的目录

    D:\APMServ5.2.6\www\htdocs\xyhy\templets\plus guestbook.rar   文件 里面  DEDE留言簿的插件:

  7. [Python笔记]第十篇:模块续

    requests Python标准库中提供了:urllib等模块以供Http请求,但是,它的 API 太渣了.它是为另一个时代.另一个互联网所创建的.它需要巨量的工作,甚至包括各种方法覆盖,来完成最简 ...

  8. MLlib 编程指导-spark-1.2.0

    本文来自 http://spark.apache.org/docs/latest/mllib-guide.html 官方文档翻译 个人翻译 MLlib包括的算法和工具主要有:分类,回归,聚类,协同过滤 ...

  9. Tomcat中配置多个端口

    在tomcat的conf/server.xml中,配置多个端口,如下: <?xml version="1.0"?> <!--应用1,端口port="80 ...

  10. yo angualr-fullstatck 项目打包部署

    yoeman使用grunt进行打包部署,直接运行grunt命令即可,期间会对代码进行检查,如果存在不规范的地方jshint会指定出来. grunt会对静态资源进行打包而且对资源文件名进行了MD5作为版 ...