Hi All,
I’ve been working on getting a date range filter working on a record database i have.
I was finally able to get it working(with some modifications) following this Tutorial.
Everything was created with Gii with some slight mods.
I can sort by the date range now but the problem is when i try to use other filters in combination with this. They don’t work. I can either use the normal filters set up.
The date range filters datef
Here’s the search() method in my model:
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);
$criteria->compare('record_id',$this->record_id);
$criteria->compare('date_added',$this->date_added);
$criteria->compare('user',$this->user,true);
$criteria->compare('note',$this->note,true);
$criteria->compare('tag',$this->tag,true);
if(!empty($this->from_date) && empty($this->to_date)){
$criteria->condition = "DATE(date_added) >= '$this->from_date'"; // date is database date column field
}elseif(!empty($this->to_date) && empty($this->from_date)){
$criteria->condition = "DATE(date_added) <= '$this->to_date'";
}elseif(!empty($this->to_date) && !empty($this->from_date)){
$criteria->condition = "DATE(date_added) >= '$this->from_date' and DATE(date_added) <= '$this->to_date'";
}
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
Here’s my Controller:
public function actionViewall()
{
$model=new Notes('search');
$model->unsetAttributes(); // clear any default values
if(!empty($_POST)){
}
if(isset($_GET['Notes']))
$model->attributes=$_GET['Notes'];
$this->render('viewall',array(
'model'=>$model,
));
}
My CGridView:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'notes-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'htmlOptions'=>array('style'=>'cursor: pointer;'),
'selectionChanged'=>"function(id){window.location='" . Yii::app()->urlManager->createUrl('', array('notes'=>'')) . "/' + $.fn.yiiGridView.getSelection(id);}",
'cssFile'=>false,
'columns'=>array(
'user',
//'date_added',
array(
'name'=>'datef',
'filter'=>'From' . CHtml::activeTextField($model,'from_date') . 'To' . CHtml::activeTextField($model,'to_date'),
),
array(
'name'=>'note',
'value'=>'strlen($data->note)>50 ? substr($data->note,0,50) . " . . ." : $data->note',
'htmlOptions'=>array('width'=>'300'),
),
array(
'name'=>'tag',
'value'=>'$data->tags->tag',
'filter' => CHtml::listData(Tags::model()->findAll(), 'id', 'tag'),
),
array(
'class'=>'CButtonColumn',
),
),
));
Any Thoughs?
Thanks!
Oliver