yii criteria select column as 与 时间段查询
需要查询某时间段的记录,但是数据库里只有一个时间记录,如果写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 与 时间段查询的更多相关文章
- Yii数据库操作增删改查-[增加\查询\更新\删除 AR模式]
在Yii的开发中常常需要去使用Yii的增删改查方法,这些方法又可以多次变化和组合,带来全方位的实现对数据库的处理,下面对这些方法做一些简单的整理和梳理,有遗漏或是BUG,敬请指出.灰常感谢!!! 一. ...
- jeecg 扩展封装查询条件 时间段查询
使用jeecg框架开发的小伙伴们知道,添加查询条件,通常是我们加一个配置(query="true")就可以将该字段设置为查询条件.简单方便.但是这样的配置查询条件仅适用于输入框输入 ...
- (后端)sql server 按时间段查询
百度的资料,保存下来: 在写按时间段查询的sql语句的时候 一般我们会这么写查询条件: where date>='2010-01-01' and date<='2010-10-1'. 但是 ...
- select * 比select column快很多奇怪案例分析
遇到MYSQL傻傻的地方,下面给个案例,大家感受下: 注意以下两个sql只有select *和select g.id区别. SQL1:SELECT g.idFROM table1 gINNER JOI ...
- bootstrap-table前端实现多条件时间段查询数据
实现思路:通过正则匹配到字段是否符合条件,时间段转换为时间戳比对. 这是大体的效果图: 页面的html代码 <div class="content-head mgb10"&g ...
- 【MYSQL】update/delete/select语句中的子查询
update或delete语句里含有子查询时,子查询里的表不能在update或是delete语句中,如含有运行时会报错:但select语句里含有子查询时,子查询里的表可以在select语句中. 如:把 ...
- 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` ...
- SQL Fundamentals: 子查询 || WHERE,HAVING,FROM,SELECT子句中使用子查询,WITH子句
SQL Fundamentals || Oracle SQL语言 子查询(基础) 1.认识子查询 2.WHERE子句中使用子查询 3.在HAVING子句中使用子查询 4.在FROM子句中使用子查询 5 ...
- EFCore扩展Select方法(根据实体定制查询语句)
EFCore扩展Select方法(根据实体定制查询语句) 通常用操作数据库的时候查询返回的字段是跟 我们的定义的实体是不一致的,所以往往针对UI或者接口层创建大量的Model, 而且需要手动对应字段 ...
随机推荐
- 32.Spring-对象依赖.md
[toc] 1.对象依赖的分类 Spring中,给对象属性赋值的方法: 构造函数 Set方法 p命名空间 自动装配 注解 1.1构造函数 构造方法通过配置文件中constructor-arg标签实现, ...
- [LeetCode OJ] Max Points on a Line—Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
//定义二维平面上的点struct Point { int x; int y; Point(, ):x(a),y(b){} }; bool operator==(const Point& le ...
- 【转】Hibernate和ibatis的比较
1. 简介 Hibernate是当前最流行的O/R mapping框架.它出身于sf.net,现在已经成为Jboss的一部分了.iBATIS是另外一种优秀的O/R mapping框架,现已改名叫myB ...
- TCP/IP-TCP
Don't cry over spilt milk. "覆水难收" 参考资料:TCP/IP入门经典 (第五版) TCP/IP详解 卷一:协议 TCP是协议栈中非常重要的一个部分, ...
- 表单数据校检方法 onsubmit()的使用?
在项目中为一个表单(from)编写onsubmit()脚本的时候,经常需要验证表单中数据的合法性 所以常会写道:<form action="/admin/addUser.do" ...
- Samba 服务器介绍
Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成.SMB(Server Messages Block,信息服务块)是一种在局域网上共享文件和打印机的一种通 ...
- 探究Android中Listview显示错乱问题
问题 最近在项目中遇到过一个很棘手的问题,就是ListView在滑动后就莫名其妙的显示错乱,网上查阅资料后问题很容易的就解决了,但是对于问题产生的原因仍是一知半解,所以不甘心的我定下心来,狠读源码,终 ...
- python中如何用sys.excepthook来对全局异常进行捕获、显示及输出到error日志中
使用sys.excepthook函数进行全局异常的获取. 1. 使用MessageDialog实现异常显示: 2. 使用logger把捕获的异常信息输出到日志中: 步骤:定义异常处理函数, 并使用该函 ...
- WordPress网站更换老鹰主机详细操作
眼看着之前买的虚拟主机就要到期了,本着节约至上的美德,就和同事一起买了老鹰主机.因为第一次网站的配置是一个朋友帮忙的,所以现在想完全自己动手操作,毕竟之后的博客维护还是得靠自己.下面就来和我一起学习怎 ...
- java安全HTTPS工具类
import java.io.FileInputStream; import java.security.KeyStore; import java.security.SecureRandom; im ...