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, 而且需要手动对应字段 ...
随机推荐
- 如何使用LoadRunner监控Windows
1.监视连接前的准备工作 1)进入被监视windows系统,开启以下二个服务Remote Procedure Call(RPC) 和Remote Registry Service (开始—)运行 ...
- 1 Two Sum(找和为target的两个数字下标Medium)
题目意思:给一个数组,找到和为target的两个元素的序号,并且只有一组这样的元素 思路:map<int,int>(nums[i],i+1),然后从后往前循环,用count找,比较i+1 ...
- C#委托基础
转载自 http://woshixy.blog.51cto.com/5637578/1070976 C#委托基础1——委托基础 委托和其委托的方法必须具有相同的签名.签名相同:1.参数类型 ...
- php常用的排序算法与二分法查找
一 : 归并排序 将两个的有序数列合并成一个有序数列,我们称之为"归并".归并排序(Merge Sort)就是利用归并思想对数列进行排序.根据具体的实现,归并排序包括"从 ...
- 转发:[Python]内存管理
本文为转发,原地址为:http://chenrudan.github.io/blog/2016/04/23/pythonmemorycontrol.html 本文主要为了解释清楚python的内存管理 ...
- C++学习笔记3——类的封装(1)
封装: 1.把属性和方法进行封装. 2.对属性和方法进行访问控制. class和struct的区别: class和struct的唯一区别是默认的访问权限不一样.struct的默认访问权限是public ...
- 转:WebSocket与Java
Bozhidar Bozhanov是Ontotext AD的高级软件工程师,拥有多年的从业经验,也是stackoverflow上的活跃用户.他精通于Java与Java技术栈,如Spring.JPA.J ...
- angular2 学习笔记 (Typescript)
1.接口奇葩验证 interface Abc { name : string } function abc(obj : Abc) { } let ttc = { name: "adad&qu ...
- Period
uvalive3026:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&pag ...
- VC++大数据量绘图时无闪烁刷屏技术实现(我的理解是,在内存上作画,然后手动显示,而不再直接需要经过WM_PAINT来处理了)
http://hantayi.blog.51cto.com/1100843/383578 引言 当我们需要在用户区显示一些图形时,先把图形在客户区画上,虽然已经画好但此时我们还无法看到,还要通过 程序 ...