Yii从日期到标准的搜索表单

时间:2014-05-07 12:03:09

标签: date search yii frameworks

我尝试创建搜索表单,按日期列出数据,从 - 到标准。 我是Yii的先生, 我的任务是下一步:

我有模特:

public $from_date;
public $to_date;

规则:

public function rules()
    {
        return array(                 
                array('AppUserID','length', 'max'=>11),
                array('AUUsername','length', 'max'=>45),


                array('AUActionTime','type','type'=>'datetime','datetimeFormat'=>'yyyy-mm-dd hh:mm:ss'),


                array('AppUserID,AUUsername, from_date, to_date', 'safe', 'on'=>'search'),

        );
    }

public function attributeLabels()
    {
        return array(
                'ActionID' => 'ID',
                'AUUsername' => 'Naziv korisnika',
                'AUActionTime' => 'Vrijeme akcije',
         );
    }

public function search()
    {

        $criteria=new CDbCriteria;
        $criteria->compare('APUsername',$this->AUUsername,true);

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

        $Date = date('yyyy-mm-dd hh:mm:ss',strtotime($this->AUActionTime));
        $startOfDay = $Date . '2014-03-12 00:00:00';
        $endOfDay = $Date . '2014-03-15 23:59:59';
        $criteria->addBetweenCondition('AUActionTime', strtotime($startOfDay) , strtotime($endOfDay) ); 



        return new CActiveDataProvider($this, array(
                'criteria'=>$criteria,
        ));
    }

我有下一个控制器:

public function actionListaexample()
    {


            $model = new Example;

            $this->layout='column1';
            if(isset($_POST['Example']))
        {
                     $model->attributes=$_POST['Example'];


            if($model->save())
                 $this->redirect(array('view','id'=>$model->AppUserID));
        }
        // renders the view file 'protected/views/site/index.php'
        // using the default layout 'protected/views/layouts/main.php'
        $this->render('listaexample',array('model'=>$model));


       }

我有标准列表的下一个视图,其中应显示数据:

<?php
Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
    $('.search-form').toggle();
    return false;
});<br />
$('.search-form form').submit(function(){
    $('#application-grid').yiiGridView('update', {
        data: $(this).serialize()
    });
    return false;
});
");
?>

<?php echo CHtml::link('Napredna pretraga','#',array('class'=>'search-button')); ?>
<div class="search-form" style="display:none">
<?php $this->renderPartial('_search1',array(
    'model'=>$model,
)); ?>
</div>

<?php 
//$gridDataProvider = new CArrayDataProvider(array(
    //array('id'=>'ActionID', 'name'=>'ActionName'),
//    
//));
 $this->widget('bootstrap.widgets.TbGridView',array(
    'type'=>'striped bordered condensed',
    'id'=>'application-grid',
    'dataProvider'=>$model->search(),
    'template'=>"{items}",
    //'filter'=>$model,
    'columns'=>array(

        //'ActionID',
          'AUUsername',
              'AUActionTime',


        array(
            'class'=>'bootstrap.widgets.TbButtonColumn',
                        'template'=>'{update}{view}{delete}',
                        'buttons'=>array(
                                        'update'=>array(
                                                        'visible'=>'false',
                                                ),
                                        'view'=>array(
                                                        'visible'=>'true',
                                                ),
                                        'delete'=>array(
                                                        'visible'=>'false',
                                                ),
                                        ),

                        'htmlOptions'=>array('style'=>'width: 50px'),
        ),
    ),
));
?>

这是我使用搜索输入字段进行搜索的视图:

<?php Yii::app()->clientScript->registerCoreScript('jquery'); ?>
<?php
/* @var $this ApplicationController */
/* @var $model Application */
/* @var $form CActiveForm */
?>

<div class="wide form">

<?php $form=$this->beginWidget('CActiveForm', array(
    'action'=>Yii::app()->createUrl($this->route),
    'method'=>'get',
        'enableAjaxValidation'=>false,
        //$model->search(),



)); ?>



    <div class="row">
        <?php echo $form->label($model,'AUUsername'); ?>
        <?php echo $form->textField($model,'AUUsername',array('size'=>45,'maxlength'=>45)); ?>
    </div>





   <div class="row">

        <?php echo $form->label($model,'AUActionTime'); ?>
        <?php //echo $form->textField($model,'AUActionTime'); ?>

                <?php echo $form->dateField($model,'AUActionTime'); ?>
    </div>




    <div class="row buttons">
        <?php echo CHtml::submitButton('Search'); ?>
    </div>

<?php $this->endWidget(); ?>

</div>

我只是尝试创建输入字段,我将日期信息放在任何格式yyyy-mm-dd我知道使用格式,并与数据库存在数据进行比较。

我放了很多代码但是任务不会让高级用户复杂化。这只是搜索表单,但在我的项目中它不起作用。感谢您的帮助

1 个答案:

答案 0 :(得分:0)

search()函数$startOfDay$endOfDay中包含2个相互连接的日期,这将产生不正确的输出格式,如2014-05-07 14:35:122014-03-12 00:00:00。该值不会使用strtotime解析。

public function search()
{
    $criteria=new CDbCriteria;
    // ...
    $Date = date('yyyy-mm-dd hh:mm:ss',strtotime($this->AUActionTime));
    $startOfDay = $Date . '2014-03-12 00:00:00';
    $endOfDay = $Date . '2014-03-15 23:59:59';
    $criteria->addBetweenCondition('AUActionTime', strtotime($startOfDay) , strtotime($endOfDay) ); 
    // ...
}