Yii search method get don't work and don't

2019-03-04 18:35发布

问题:

Hi yesterday i tried one way to create search by datetime, and you can see link: Search task on the next post. Today I try one another way: When I succed i will put sollution back thank you.

This is my search file:

<?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
        //datepicker for date_from 
        echo CHtml::label("From date", 'datepicker');
        $this->widget('zii.widgets.jui.CJuiDatePicker', array(
            'name' => 'filters[date_from]',
            //'value' => $filters['date_from'],
            // additional javascript options for the date picker plugin
            'options' => array(
                    'showButtonPanel' => true,
                'showAnim' => 'slide', //'slide','fold','slideDown','fadeIn','blind','bounce','clip','drop'
                'dateFormat'=>'yyyy-mm-dd hh:mm:ss',
                ),
            'htmlOptions' => array(
                'id'=>'date_from',  
                ),
            ));?>
        </div>
        <div class="row">
            <?php
                //datepicker for date to
            echo CHtml::label("To date", 'datepicker');
            $this->widget('zii.widgets.jui.CJuiDatePicker', array(
                    'name' => 'filters[date_to]',
                //'value' => $filters['date_to'],
                // additional javascript options for the date picker plugin
                'options' => array(
                    'showButtonPanel' => true,
                    'showAnim' => 'slide', //'slide','fold','slideDown','fadeIn','blind','bounce','clip','drop'
                    'dateFormat'=>'yyyy-mm-dd hh:mm:ss',
                ),
                'htmlOptions' => array(
                    'id'=>'date_to',    
                    ),
                ));?>
        </div>
    <div class="row buttons">
        <?php echo CHtml::submitButton('Search'); ?>
    </div>
    <?php $this->endWidget(); ?>
</div>

This is my model file:

<?php
class AppLog extends AltActiveRecord 
{
    private $_ActionName=null;

    public $date_from;
    public $date_to;

    public function getDbConnection(){
        return Yii::app()->connectionManager->getConnection(Yii::app()->user->getState('application'));
    } 

    public function relations ()
    {
        return array (
            'actions'=>array(self::HAS_ONE, 'Actions', array('ActionID'=>'ActionID')),
        );  
    }   

    public function getActionName()
    {
        if ($this->_ActionName === null && $this->actions !== null)
        {
            $this->_ActionName = $this->actions->ActionName;
        }
        return $this->_ActionName;
    }

    public function setActionName($value)
    {
        $this->_ActionName = $value;
    }  

    public function tableName()
    {
            return 'applog';
    }

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

                // The following rule is used by search().
                // @todo Please remove those attributes that should not be searched.
                array('AppUserID,AUUsername, date_from, date_to', '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);
        $criteria->compare('date_from',$this->date_from,true);
        $criteria->compare('date_to',$this->date_to,true);     
        return new CActiveDataProvider($this, array(
                'criteria'=>$criteria,
        ));
    }

    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }
}
?>

THIS IS MY CONTROLLER ACTION FOR SEARCH

public function actionLista()
{
    $model = new Datalist;
    $this->layout='column1';
    //

    if($_GET!=null) 
    {
        $date_from = $_GET['filters']['date_from'];

        $params[':date_from'] = date('yyyy-mm-dd hh:mm:ss', strtotime($_GET['filters']['date_from']));
        $date_to = $_GET['filters']['date_to'];
        $params[':date_to'] = date('yyyy-mm-dd hh:mm:ss', strtotime($_GET['filters']['date_to']));

        if($date_from == '') $params[':date_from'] = date('yyyy-mm-dd hh:mm:ss', strtotime('2014-01-01 00:00:00'));
        if($date_to == '') $params[':date_to'] = date('yyyy-mm-dd hh:mm:ss', strtotime('2999-01-01 00:00:00'));
        //$condition = '(AUActionTime>:date_from OR AUActionTime<:date_to)';


        //set filters
        //$this->setFilters($_GET['filters']);
        //die(CVarDumper::dump($params,10,true));                        
    }
    else {
        $this->filters = array(
        'date_from' => date('yyyy-mm-dd hh:mm:ss', strtotime('2014-01-01')),
        'date_to' => date('yyyy-mm-dd hh:mm:ss', strtotime('today + 1 day')),
        );
    }

回答1:

1) In your controller you should set the scenario as "search" like this

 $model = new Datalist('search');
 $model->unSetAttributes();

2) You need to assign the $_GET values to the model before view is rendered like this

// If your get vars are different then accordingly
if(isset($_GET['AppLog'])){
     $model->attributes = $_GET['AppLog'];

....
  $model->date_to = $date_to // After you retrieved and formatted from $_GET as above 
  $model->date_from = $date_from

Your controller action does not seem to rendering a view; i am assuming that you have omitted it here, but you are displaying the output in someway