日期字段搜索无法在CGridView - YII框架中工作

时间:2013-12-24 09:50:05

标签: yii

我正在尝试在CGridView数据中搜索Date字段,我无法在给定日期的网格中获取搜索值。

我的模特搜索(),

public function search()
    {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('crm_base_contact_id',$this->crm_base_contact_id);
        $criteria->compare('name',$this->name,true);
        $criteria->compare('created',$this->created);       
        $criteria->compare('createdby',$this->createdby,true);
        $criteria->compare('description',$this->description);
        $criteria->compare('is_active',$this->is_active);
        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }

我的CJuiDatePicker的网格视图,

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'basecontact-grid',
    'dataProvider'=>$model->search(),
    //'filter'=>$model,
    'columns'=>array(
        array('class'=>'CButtonColumn',),
        'name',
        'crm_base_contact_id',      
                   array(
            'name'=>'is_active',
            'filter'=>CHtml::dropDownList('Event[is_active]', '',  
                array(
                    ''=>'',
                    '1'=>'Y',
                    '0'=>'N',
                )
            ),
            'value' =>'($data->is_active==1)?"Y":"N"',
        ),
        array(  'name'=>'created',
                'header'=>'created',
                'value' => '$data->createdDate',),
        'createdby',
        'description',
    ),
)); ?>
模型中的

createdDate()格式化日期,如“dd / mm / yyyy”,

function getCreatedDate()
    {

      if ($this->created === null || $this->created === ('0000-00-00 00:00:00'))
        return "";

      return Yii::app()->dateFormatter->format("d/M/y", $this->created);
    }

enter image description here

3 个答案:

答案 0 :(得分:1)

怎么样?

$Date = date("Y-m-d",strtotime($this->created)); // get proper Y-m-d
$startOfDay = $Date . ' 00:00:00'; // from start of the day
$endOfDay = $Date . ' 23:59:59';   // until end of the day

// the rows between these
$criteria->addBetweenCondition('created', strtorime($startOfDay) , strtotime(endOfDay) );

答案 1 :(得分:1)

在模型的搜索功能中,我更改了以下行:

  $criteria->compare('fecha',$this->fecha,true);

通过这个漫长而有效的代码:

  if ($this->fecha) { // this way with avoid the empty search grid
    if (strlen($this->fecha) == 10) // full date entered
      $criteria->compare('fecha', date("Y-m-d", strtotime($this->fecha)));
    elseif (strlen($this->fecha) > 2) { // dd-mm entered
      $this->fecha = $this->fecha . date("-Y");
      $criteria->compare('fecha', date("Y-m-d", strtotime($this->fecha)));
    } else { // dd only entered
      $this->fecha = $this->fecha . date("-m-Y");
      $criteria->compare('fecha', date("Y-m-d", strtotime($this->fecha)));
    }
}

答案 2 :(得分:0)

在搜索()

中尝试此操作
   $criteria->compare('created',date("Y-m-d",strtotime($this->created),true);

而不是

   $criteria->compare('created',$this->created);