yii criteria select column as 与 时间段查询

时间:2023-03-09 22:07:21
yii criteria select column as 与 时间段查询

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

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

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

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

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

yii criteria select column as 与 时间段查询

我这里的具体代码如下,当然,是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',
),*/
),
)); ?>