Yii Criteria与数组而不是数据库进行比较

时间:2015-03-30 13:45:00

标签: php arrays yii criteria cgridview

我所拥有的Yii应用程序链接到另一个应用程序,我想要做的是使用我在CGridView中使用的条件功能,将值与数组而不是数据库进行比较(如果有意义的话)。

所以换句话说我想拥有它,如果用户在CGridView过滤器中输入一个单词,那么它会将该单词与我在search()函数中提供的数组进行比较。

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'project-grid',
    'rowCssClassExpression'=>'"items[]_{$data->id}"',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'ajaxUpdate'=>'true',
    'columns'=>array(
        array(
            'name'=>'Form Name',
            'value'=>'CHtml::link(ucwords(str_replace("publish", "", str_replace("-", " ", $data->form_name))), "/request/update/$data->primaryKey")',
            'filter'=>CHtml::textField('request[form_name]','', array('class'=>'form-control', 'placeholder'=>'Form Name...')),
            'type'=>'raw'
        ),

        array(
            'name'=>'User Name',
            'value'=>'CHtml::link(ucfirst($data->user_name), "/request/update/$data->primaryKey")',
            'filter'=>CHtml::textField('request[user_name]','', array('class'=>'form-control', 'placeholder'=>'User Name...')),
            'type'=>'raw'
        ),
        array(
            'name'=>'Company',
            'value'=>'CHtml::link(frontuser::model()->getCompany($data->user_id), "/request/update/$data->primaryKey")',
            'filter'=>CHtml::textField('request[company]','', array('class'=>'form-control', 'placeholder'=>'Company Name...')),
            'type'=>'raw'
        ),
        array(
            'name'=>'Last Updated',
            'value'=>'CHtml::link(date("d/m/Y", strtotime($data->updated)), "/request/update/$data->primaryKey")',
            'filter'=>CHtml::textField('request[updated]','', array('class'=>'form-control', 'placeholder'=>'Last Updated...')),
            'type'=>'raw'
        ),
        array(
            'name'=>'DKA Ref',
            'value'=>'CHtml::link($data->dka_ref, "/request/update/$data->primaryKey")',
            'filter'=>CHtml::textField('request[dka_ref]','', array('class'=>'form-control', 'placeholder'=>'DKA Reference...')),
            'type'=>'raw'
        ),
        array(
            'name'=>'Required By',
            'value'=>'request::model()->getRequiredDate($data->data)',
            'filter'=>'',
            'type'=>'raw'
        ),
        array(
            'name'=>'Status',
            'value'=>'request::model()->getStatusColor($data->primaryKey)',
            'filter'=>'
        <!--<div class="circle green-circle filter-circle"></div>
            <div class="circle amber-circle filter-circle margin-left"></div>
            <div class="circle red-circle filter-circle margin-left-60"></div>-->

            '.CHtml::dropDownList('request[status]', '12', array(''=>'','3'=>'Green','2'=>'Amber', '1'=>'Red'), array('class'=>'request-filter form-control')),
            'type'=>'raw'
        ),


        array(
            'class'=>'CButtonColumn',
            'template'=>'{delete}',
            'buttons'=>array(
                'delete' => array(
                    'url'=>'$this->grid->controller->createUrl("request/delete/$data->primaryKey")',
                ),
            ),
        ),

    )
));

这是我的search()函数,它被传递给CGridView

 public function search(/*$id = null*/){

    $criteria=new CDbCriteria;
    $criteria->compare('form_name',$this->form_name, true);
    $criteria->compare('user_name',$this->user_name, true);
    $criteria->compare('last_updated',$this->updated, true);
    $criteria->compare('dka_ref',$this->dka_ref, true);
    $criteria->compare('status', $this->status);
    $criteria->addInCondition('company', array('0'=>'The Company')); // <-- this is what I would like to be able to do
    $criteria->addCondition('status>0');
    $criteria->order = 'id DESC';
    $dataProvider=new CActiveDataProvider('request', array('criteria'=>$criteria, 'pagination'=>array(
                    'pageSize'=>'50',
                    'route'=>'request/index'
                    )));
   /* foreach($dataProvider->data*/
    return $dataProvider;
}

谢谢你们:)

1 个答案:

答案 0 :(得分:0)

我认为你不需要在这里,但最好使用连接条件。

如果您加入公司名称,您只需搜索那些导致类似查询的内容。

(companyFilter是瞬态属性,它仅用于过滤,我对您的数据模型做出一些假设; - ))

    if ( isset($this->companyFilter) && !empty($this->companyFilter)) {

        $criteria->join = 'join tb_Company company on company.idCompany = t.idCompany';
        $criteria->compare('company.name',$this->companyFilter);

    }

对我而言,使用$ criteria-&gt;加入而不是$ criteria-&gt;和一段关系感觉有点脏,但我还没有使用这种方法。