简体   繁体   中英

Filtering data in CGridView based on relations

Hi everyone, I am new to yii and I am performing crud operation using gii for model called city and I have relation with state model a city->state_id = state.state_id. Crud everything is working fine, but in that view part I want to filter based on state_name

In relations method,I have code this code

'state' => array(self::BELONGS_TO, 'MasState', 'State_Id'),

here is the code for search

public function search() {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria = new CDbCriteria;
        $MasState = new MasState;


        $criteria->together = true;
       $criteria->compare('state.State_Name',$MasState->State_Name,true);  
      $criteria->with= array('state');
        $criteria->compare('City_Id', $this->City_Id);
        $criteria->compare('State_Id', $this->State_Id);
        $criteria->compare('City_Name', $this->City_Name, true);
        $criteria->compare('Del_Flag', $this->Del_Flag);
        $criteria->compare('Ts_Modified', $this->Ts_Modified, true);
        $criteria->compare('Ts_Created', $this->Ts_Created, true);

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

and admin.php page code

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'mas-city-grid',
    'dataProvider'=>$model->search(),
'filter'=>$model,
    'columns'=>array(
        array(
            'header' => 'SL NO#',
            'class' => 'CounterColumn'
        ),
        array(
          'name' => 'state.State_Name',
          'header' => 'State Name',
          'filter' => CHtml::activeTextField($model,'State_Name'),
          'value' => '$data->state->State_Name',
        ),
        'City_Name',
        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); ?>

I am getting error "Property "MasCity.State_Name" is not defined" I know city model does not have state_name but how to make it work. Plese help me

Thanks in advance

First in your search method you should do:

public function search() {

    $criteria = new CDbCriteria;
    $criteria->with= array('state');
    $criteria->together = true;


    //Get the relation value from the request
    $criteria->compare('state.State_Name',Yii::app()->request->getParam('state_state_name'),true);  
    $criteria->compare('City_Id', $this->City_Id);
    $criteria->compare('State_Id', $this->State_Id);
    $criteria->compare('City_Name', $this->City_Name, true);
    $criteria->compare('Del_Flag', $this->Del_Flag);
    $criteria->compare('Ts_Modified', $this->Ts_Modified, true);
    $criteria->compare('Ts_Created', $this->Ts_Created, true);

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

And in your view:

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'mas-city-grid',
    'dataProvider'=>$model->search(),
'filter'=>$model,
    'columns'=>array(
        array(
            'header' => 'SL NO#',
            'class' => 'CounterColumn'
        ),
        array(
          'name' => 'state.State_Name',
          'header' => 'State Name',
          'filter' => CHtml::textField('state_state_name', Yii::app()->request->getParam('state_state_name')),
          'value' => '$data->state->State_Name',
        ),
        'City_Name',
        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM