大家好, 我是yii的新手,我正在使用gii进行crud操作,称为城市模型,我与州模型有关系 - > state_id = state.state_id。 Crud一切正常,但在该视图中我想根据state_name进行过滤
在关系方法中,我已编码此代码
'state' => array(self::BELONGS_TO, 'MasState', 'State_Id'),
这是搜索的代码
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,
));
}
和admin.php页面代码
<?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',
),
),
)); ?>
我收到错误“属性”MasCity.State_Name“未定义”我知道城市模型没有state_name但是如何使其工作。请帮助我
提前致谢
答案 0 :(得分:2)
首先应该使用search
方法:
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,
));
}
在你看来:
<?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',
),
),
)); ?>