Yii搜索方法get不起作用,不与数据库中的数据进行比较 [英] Yii search method get don't work and don't compare with data in database

查看:15
本文介绍了Yii搜索方法get不起作用,不与数据库中的数据进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我尝试了一种按日期时间创建搜索的方法,您可以看到链接:搜索任务 在下一篇文章中.今天我尝试另一种方式:当我成功时,我会把解决方案放回去,谢谢.

这是我的搜索文件:

clientScript->registerCoreScript('jquery');?><?php/* @var $this ApplicationController *//* @var $model 应用程序 *//* @var $form CActiveForm */?><div class="宽表单"><?php $form=$this->beginWidget('CActiveForm', array('action'=> Yii::app()->createUrl($this->route),'方法'=>'获取','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 class="row"><?php//date_from 的日期选择器echo CHtml::label("From date", 'datepicker');$this->widget('zii.widgets.jui.CJuiDatePicker', array('名称' =>'过滤器[date_from]',//'值' =>$filters['date_from'],//日期选择器插件的附加 javascript 选项'选项' =>大批('showButtonPanel' =>真的,'showAnim' =>'slide',//'slide','fold','slideDown','fadeIn','blind','bounce','clip','drop''dateFormat'=''yyyy-mm-dd hh:mm:ss',),'htmlOptions' =>大批('id'=''date_from',),));?>

<div class="row"><?php//日期选择器echo CHtml::label("截止日期", 'datepicker');$this->widget('zii.widgets.jui.CJuiDatePicker', array('名称' =>'过滤器[date_to]',//'值' =>$filters['date_to'],//日期选择器插件的附加 javascript 选项'选项' =>大批('showButtonPanel' =>真的,'showAnim' =>'slide',//'slide','fold','slideDown','fadeIn','blind','bounce','clip','drop''dateFormat'=''yyyy-mm-dd hh:mm:ss',),'htmlOptions' =>大批('id'=''date_to',),));?>

<div class="行按钮"><?php echo CHtml::submitButton('Search');?>

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

这是我的模型文件:

connectionManager->getConnection(Yii::app()->user->getState('application'));}公共职能关系 (){返回数组('actions'=>array(self::HAS_ONE, 'Actions', array('ActionID'=>'ActionID')),);}公共函数 getActionName(){if ($this->_ActionName === null && $this->actions !== null){$this->_ActionName = $this->actions->ActionName;}返回 $this->_ActionName;}公共函数 setActionName($value){$this->_ActionName = $value;}公共函数表名(){返回应用程序";}公共函数规则(){返回数组(数组('AppUserID','length', 'max'=>11),数组('AUUsername','length', 'max'=>45),array('AUActionTime','type','type'=>'datetime','datetimeFormat'=>'yyyy-mm-dd hh:mm:ss'),//以下规则由 search() 使用.//@todo 请删除那些不应搜索的属性.数组('AppUserID,AUUsername, date_from, date_to', 'safe', 'on'='search'),);}公共函数attributeLabels(){返回数组('ActionID' =>'ID','AU用户名' =>'Naziv korisnika','AUActionTime' =>'Vrijeme akcije',);}公共功能搜索(){$criteria=新的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);返回新的 CActiveDataProvider($this, array('标准'=>$标准,));}公共静态函数模型($className=__CLASS__){return parent::model($className);}}?>

这是我的控制器搜索操作

公共函数 actionLista(){$model = 新数据列表;$this->layout='column1';//如果($_GET!=空){$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)';//设置过滤器//$this->setFilters($_GET['filters']);//die(CVarDumper::dump($params,10,true));}别的 {$this->filters = array('date_from' =>日期('yyyy-mm-dd hh:mm:ss', strtotime('2014-01-01')),'date_to' =>date('yyyy-mm-dd hh:mm:ss', strtotime('今天 + 1 天')),);}

解决方案

1) 在您的控制器中,您应该像这样将场景设置为搜索"

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

2) 在像这样呈现视图之前,您需要将 $_GET 值分配给模型

//如果您的 get 变量不同,则相应地if(isset($_GET['AppLog'])){$model->attributes = $_GET['AppLog'];....$model->date_to = $date_to//如上所述从 $_GET 检索并格式化之后$model->date_from = $date_from

您的控制器操作似乎没有呈现视图;我假设你在这里省略了它,但你以某种方式显示输出

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

这篇关于Yii搜索方法get不起作用,不与数据库中的数据进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆