Yii search form by date from to criteria

2019-03-06 01:33发布

I try to create search form to list data from database by date, from - to criteria. I'm Beginer in Yii, And my task is next:

I have model:

public $from_date;
public $to_date;

Rules:

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,
        ));
    }

I have next controller:

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));


       }

And I have next view for standard list where data should be presented:

<?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'),
        ),
    ),
));
?>

This is view for my search with search input fields:

<?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>

I just try to make input fields where I put info about date any format yyyy-mm-dd i know work with formats, and to make comparation with database exist data.

I put realy much code but task is not to complicate for advanced users. That is only search form but in my project it don't work. Thank you for any help

1条回答
淡お忘
2楼-- · 2019-03-06 02:01

In the search() function $startOfDay and $endOfDay contains 2 dates concatenated to each other, which will produce an incorrect output format like 2014-05-07 14:35:122014-03-12 00:00:00. That value won't parse right with 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) ); 
    // ...
}
查看更多
登录 后发表回答