使用OR ||进行Yii CGridview模型搜索

时间:2014-02-21 16:33:50

标签: php search yii cgridview

我创建了一个高级搜索表单,该表单将使用ORIN条件搜索模型。但是它无法更新CGridView,因为它返回模型属性的数组。如果我从gridview中删除这些列然后它将工作,但我需要这些列在gridview上可见。

模型

public function weeklystatus($arr = array())
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        //$criteria->compare('id',$this->id);
        $criteria->compare('PROJECT',$this->PROJECT,true);
        if(!empty($arr) && isset($arr['PROJCODE'])){
            $str = '';
            foreach($arr['PROJCODE'] as $value) {
                $str .= "PROJCODE = '$value' || ";
            }           
            $criteria->addCondition(substr($str, 0, -3));
        }else
            $criteria->compare('PROJCODE',$this->PROJCODE,true);
        $criteria->compare('PROJID',$this->PROJID);
        $criteria->mergeWith($this->dateRangeSearchCriteria('StartDATE', $this->StartDATE));        
        $criteria->mergeWith($this->dateRangeSearchCriteria('ProjectEndDate', $this->ProjectEndDate));
        $criteria->mergeWith($this->dateRangeSearchCriteria('ActualEndDate', $this->ActualEndDate));
        $criteria->compare('PROCESSOR',$this->PROCESSOR,true);
        $criteria->compare('OFFICE',$this->OFFICE,true);
        $criteria->compare('DEPTCODE',$this->DEPTCODE,true);
        //$criteria->compare('PERCENT',$this->PERCENT,true);        
        $criteria->compare('PERCENTPlanned',$this->PERCENTPlanned,true);
        $criteria->compare('KM',$this->KM,true);
        $criteria->compare('KMPlanned',$this->KMPlanned,true);
        if(!empty($arr) && isset($arr['MC'])){
            $str = '';
            foreach($arr['MC'] as $value) {
                $str .= "MC = '$value'";
            }
            $criteria->addCondition($str);
        }else{
            $criteria->compare('MC',$this->MC,true);
        }
        $criteria->compare('MCSALE',$this->MCSALE);
        $criteria->compare('CATEGORY',$this->CATEGORY,true);
        $criteria->compare('AREA',$this->AREA,true);
        $criteria->compare('COUNTRY',$this->COUNTRY,true);
        $criteria->compare('PROJINFO',$this->PROJINFO,true);
        $criteria->compare('REGION',$this->REGION,true);
        $criteria->compare('ASAAREA',$this->ASAAREA,true);
        $criteria->compare('LORM',$this->LORM,true);

        $date = date('Y-m-d',strtotime("-2 week"));
        $criteria->addCondition("StartDATE > '2013-01-01'"); 
        $criteria->addCondition("StartDATE < '$date' AND PERCENT <100 || StartDATE > '$date' AND PERCENT =100 ");
                /*AND (StartDATE < '$date' AND PERCENT <100)
                || (StartDATE > '$date' AND PERCENT =100) ");*/
        //$criteria->addCondition("PERCENT < 100 AND StartDATE < '$date'");
        //$criteria->addCondition('StartDATE > '.$date . ' AND PERCENT >  -1' );

        $criteria->order = 'ASAAREA';


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

控制器

public function actionWeeklystatus(){       
        $model=new ViewWebprojectreport('weeklystatus');
        $model->unsetAttributes();  // clear any default values
        $globalController = new GlobalController;

        if(isset($_GET['ViewWebprojectreport'])){

            $model->attributes=$_GET['ViewWebprojectreport'];
        }

        //print_r($model->attributes);
        //exit();

        $processorList = $globalController->getProcessorList();
        $projCodeList = $globalController->getProjectcodeList();
        $asaarea = Yii::app()->db->createCommand("SELECT DISTINCT
                (   view_webprojectreport.ASAAREA   )
                FROM
                view_webprojectreport
                ORDER BY
                view_webprojectreport.ASAAREA ASC")->queryAll();
        $asaareaList = CHtml::listData($asaarea, 'ASAAREA','ASAAREA');
        $officeList = $globalController->getOfficeList();
        $deptCode = $this->getDepartmentCode();
        $deptCodeList = CHtml::listData($deptCode, 'Code','Code');
        $categoryList = $globalController->getCategoryList(array("condition"=>"category_id < 8 || (category_id>8 && category_id < 14) || category_id > 31"));
        $countryList = $globalController->getCountryList();
        $region =Yii::app()->db->createCommand("SELECT DISTINCT
                (   view_webprojectreport.REGION    )
                FROM
                view_webprojectreport
                ORDER BY
                view_webprojectreport.REGION ASC")->queryAll();
        $regionList = CHtml::listData($region, 'REGION','REGION');
        $mcList = array('MC' => "MC","P"=>"Propietary");

        Yii::app()->user->setState('exportModel',$model); //this can be any other storage

        $this->render("weekly_status",array(
                "model"=>$model,    
                'processorList'=>$processorList,
                'asaareaList'=>$asaareaList,
                'projCodeList'=>$projCodeList,
                'officeList'=>$officeList,
                'deptCodeList'=>$deptCodeList,
                'categoryList'=>$categoryList,
                'countryList'=>$countryList,
                'regionList'=>$regionList,
                'mcList'=>$mcList
                ));
    }

查看

<?php 
Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
    $('.search-form').toggle();
    return false;
});

$('.search-form form').submit(function(){
    $.fn.yiiGridView.update('view-weekly-status-grid', {
        data: $(this).serialize(),
    });
    return false;
});

",CClientScript::POS_LOAD);
?>
</head>

<h1>Weekly Status Report</h1>

<div style="font-size: 12pt;">
    <?php //echo CHtml::link('<img src="images/pdficon.gif"/>',array("ViewWebprojectreport/exportToPdf","report"=>"weeklystatus")); ?>
    <?php  echo $this->widget('zii.widgets.jui.CJuiButton', array(       
          'buttonType'=>'link',
          'name'=>'btnDownloadWeeklyStatus',
          'caption'=>'Download Weekly Status',
          'options'=>array('icons'=>'js:{primary:"ui-icon-document"}'),
            "url"=>"index.php?r=ViewWebprojectreport/exportToPdf&report=weeklystatus",
        'htmlOptions'=>array('style'=>"font-size: 12px;height: 30px;"),
            'themeUrl'=>Yii::app()->baseUrl.'/themes',//url of your themes for JUI
        'theme'=>'redmond', //theme name the above url should have a folder named by the theme name specified here eg. /protected/extensions/jui/jquery/css/redmond/
        'cssFile'=>'jquery-ui-1.10.3.custom.min.css', // the css file in the theme folder
         ),true); ?>
</div>


<div id="weekly_status_graph" style="height:800px;display:none;width: 1120px;">

</div>



<?php echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>
<div class="search-form" style="display:none">
<?php  $this->renderPartial('_search',array(
    'model'=>$model,
    'processorList'=>$processorList,
    'asaareaList'=>$asaareaList,
    'projCodeList'=>$projCodeList,
    'officeList'=>$officeList,
    'deptCodeList'=>$deptCodeList,
    'categoryList'=>$categoryList,
    'countryList'=>$countryList,
    'regionList'=>$regionList,
    'mcList'=>$mcList
)); ?>
</div><!-- search-form -->


<?php $this->widget('zii.widgets.grid.CGridView', array(
        'id'=>'view-weekly-status-grid',
        'dataProvider'=>$model->weeklystatus(),
        'filter'=>$model,
        'rowCssClassExpression'=>'($data->PERCENT == 100)? "yellow" : ($row%2?"even":"odd")',
        //'ajaxUpdate' => TRUE,
        'afterAjaxUpdate'=>'function(id, data){
            $.ajax({
                url: "index.php?r=ViewWebprojectreport/weeklystatusgraph",
                dataType: "json",
                type: "get",
                success: function(data) {
                        if(data.totalprojects>40){
                            $("#weekly_status_graph").css("width","1900px");
                            $("#weekly_status_graph").css("margin-left","-390px");

                        }else if(data.totalprojects>20){        
                            $("#weekly_status_graph").css("width","1250px");
                            $("#weekly_status_graph").css("margin-left","-50px");
                        }else{
                            $("#weekly_status_graph").css("width","1120px");
                            $("#weekly_status_graph").css("margin-left","-0px");
                        }

                        $("#weekly_status_graph").show();
                        buildColumnGraph(data);

                }
            })
        }',
        'columns'=>array(       
                //'MC',
                array(
                        "name"=>"MC",
                        "header"=>"MC/P",
                        "htmlOptions"=>array(
                                "width"=>"20px"
                        )
                ),
                //'OFFICE',
                array(
                        "name"=>"PROJCODE",
                        "header"=>"Center",
                        "htmlOptions"=>array(
                                "width"=>"15px"
                        )
                ),
                //'ASAAREA',
                array(
                        "name"=>"ASAAREA",
                        "header"=>"Region",
                        "htmlOptions"=>array(
                                "width"=>"80px"
                        )
                ),
                //'PROJECT',
                array(
                        "name"=>"PROJECT",
                        "htmlOptions"=>array(
                                "width"=>"150px"
                        )
                ),
                //'LORM',
                array(
                    "name"=>"LORM",
                    "htmlOptions"=>array(
                        "width"=>"20px"
                    )
                ),
                'CATEGORY',
                //'KM',
                array(
                        "name"=>"KM",
                        "header"=>"KM/KM<sup>2</sup>"
                        /*"htmlOptions"=>array(
                                "width"=>"20px"
                        )*/
                ),
                //PROC TYPE
                //'StartDATE',
                array(
                    "name"=>"StartDATE",
                    "htmlOptions"=>array(
                        "width"=>"65px"
                    )
                ),
                //'ProjectEndDate',
                array(
                    "name"=>"ProjectEndDate",
                    "htmlOptions"=>array(
                        "width"=>"65px"
                    )
                ),
                //current date
                /*array(
                    "header"=>"Current date",
                    "value"=>$date
                ),*/
                // planned poc
                // actiaul poc
                //expected
                //'ActualEndDate',
                array(
                    "name"=>"ActualEndDate",
                    "htmlOptions"=>array(
                        "width"=>"65px"
                    )
                ),
                array(
                    "name"=>"PROJINFO",
                    'type'=>'raw',  
                    "value"=>'( strlen($data->PROJINFO) > 200 ) ? CHtml::tag("span", array("title"=>$data->PROJINFO), CHtml::encode(substr($data->PROJINFO, 0, 200)) . "..") : CHtml::encode($data->PROJINFO)',
//                      '$data->GetTruncatedPROJINFO()' ,
                    "htmlOptions"=>array(
                        //"width"=>"300px"
                    )
                ),
                array(
                    'class'=>'CButtonColumn',
                    'template'=>'{view}',
                    'htmlOptions' => array('style'=>'width:5px'),
        ),
    ),
)); ?>

ASAAREA我创建了一个复选框,这样我就可以在1列上使用2个条件搜索模型。它返回结果,但渲染页面失败,因为我在ASAAREA上显示CGridView。我该如何解决这个问题?

如果我在视图上注释//'filter'=>$model,,它可以正常工作。但我不想这样做。

如果我离开filter未注释

,它将产生堆栈跟踪
Error 500: <h1>PHP Error [2]</h1>
<p>htmlspecialchars() expects parameter 1 to be string, array given (/opt/yii-1.1.14.f0fee9/framework/web/helpers/CHtml.php:103)</p>
<pre>#0 unknown(0): CWebApplication->handleError()
#1 /opt/yii-1.1.14.f0fee9/framework/web/helpers/CHtml.php(103): htmlspecialchars()
#2 /opt/yii-1.1.14.f0fee9/framework/web/helpers/CHtml.php(2610): encode()
#3 /opt/yii-1.1.14.f0fee9/framework/web/helpers/CHtml.php(158): renderAttributes()
#4 /opt/yii-1.1.14.f0fee9/framework/web/helpers/CHtml.php(2255): tag()
#5 /opt/yii-1.1.14.f0fee9/framework/web/helpers/CHtml.php(1434): activeInputField()
#6 /opt/yii-1.1.14.f0fee9/framework/zii/widgets/grid/CDataColumn.php(103): activeTextField()
#7 /opt/yii-1.1.14.f0fee9/framework/zii/widgets/grid/CGridColumn.php(116): CDataColumn->renderFilterCellContent()
#8 /opt/yii-1.1.14.f0fee9/framework/zii/widgets/grid/CGridView.php(532): CDataColumn->renderFilterCell()
#9 /opt/yii-1.1.14.f0fee9/framework/zii/widgets/grid/CGridView.php(510): CGridView->renderFilter()
#10 /opt/yii-1.1.14.f0fee9/framework/zii/widgets/grid/CGridView.php(480): CGridView->renderTableHeader()
#11 /opt/yii-1.1.14.f0fee9/framework/zii/widgets/CBaseListView.php(167): CGridView->renderItems()
#12 unknown(0): CGridView->renderSection()
#13 /opt/yii-1.1.14.f0fee9/framework/zii/widgets/CBaseListView.php(150): preg_replace_callback()
#14 /opt/yii-1.1.14.f0fee9/framework/zii/widgets/CBaseListView.php(135): CGridView->renderContent()
#15 /opt/yii-1.1.14.f0fee9/framework/web/CBaseController.php(173): CGridView->run()
#16 /opt/paradox/protected/views/viewWebprojectreport/weekly_status.php(314): ViewWebprojectreportController->widget()
#17 /opt/yii-1.1.14.f0fee9/framework/web/CBaseController.php(126): require()
#18 /opt/yii-1.1.14.f0fee9/framework/web/CBaseController.php(95): ViewWebprojectreportController->renderInternal()
#19 /opt/yii-1.1.14.f0fee9/framework/web/CController.php(869): ViewWebprojectreportController->renderFile()
#20 /opt/yii-1.1.14.f0fee9/framework/web/CController.php(782): ViewWebprojectreportController->renderPartial()
#21 /opt/paradox/protected/controllers/ViewWebprojectreportController.php(970): ViewWebprojectreportController->render()
#22 /opt/yii-1.1.14.f0fee9/framework/web/actions/CInlineAction.php(49): ViewWebprojectreportController->actionWeeklystatus()
#23 /opt/yii-1.1.14.f0fee9/framework/web/CController.php(308): CInlineAction->runWithParams()
#24 /opt/yii-1.1.14.f0fee9/framework/web/filters/CFilterChain.php(133): ViewWebprojectreportController->runAction()
#25 /opt/yii-1.1.14.f0fee9/framework/web/CController.php(291): CFilterChain->run()
#26 /opt/yii-1.1.14.f0fee9/framework/web/CController.php(265): ViewWebprojectreportController->runActionWithFilters()
#27 /opt/yii-1.1.14.f0fee9/framework/web/CWebApplication.php(282): ViewWebprojectreportController->run()
#28 /opt/yii-1.1.14.f0fee9/framework/web/CWebApplication.php(141): CWebApplication->runController()
#29 /opt/yii-1.1.14.f0fee9/framework/base/CApplication.php(180): CWebApplication->processRequest()
#30 /opt/paradox/index.php(31): CWebApplication->run()
</pre>

1 个答案:

答案 0 :(得分:0)

如果需要显示作为数组的模型的属性,例如,您希望使用clistview考虑网格单元格中的所有值。您的高级搜索仍然有效,您将能够为每个单元格编写自定义html。

另一个解决方案是在cgridview中呈现一个序列化您的数组属性的虚拟变量