Php CGridView过滤多个模型
我一直在寻找我的问题的答案,但两个答案都不起作用。我的问题是关于在CGridView中过滤/搜索Yii 在我的申请人模型中,我有以下内容:Php CGridView过滤多个模型,php,view,grid,yii,Php,View,Grid,Yii,我一直在寻找我的问题的答案,但两个答案都不起作用。我的问题是关于在CGridView中过滤/搜索Yii 在我的申请人模型中,我有以下内容: private $_city = null; private $province_id = null; public function getCity() { if($this->_city === null && $this->address_id !== null) { $thi
private $_city = null;
private $province_id = null;
public function getCity()
{
if($this->_city === null && $this->address_id !== null) {
$this->_city = $this->address->city;
}
return $this->_city;
}
public function setCity($value)
{
$this->_city = $value;
}
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->with = array('address'=>array('alias'=>'address'));
$criteria->compare('id',$this->id);
$criteria->compare('phn',$this->phn);
$criteria->compare('gender',$this->gender);
$criteria->compare('dob',$this->dob,true);
$criteria->compare('first_name',$this->first_name,true);
$criteria->compare('last_name',$this->last_name,true);
$criteria->compare('band_id',$this->band_id);
$criteria->compare('note',$this->note,true);
$criteria->compare('address.city',$this->city);
$criteria->compare('address.province_id',$this->province_id);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=>array(
'attributes'=>array(
'id',
'phn',
'first_name',
'last_name',
'band_id',
'city'=>array(
'asc'=>'address.city ASC',
'desc'=>'address.city DESC',
),
'province_id'=>array(
'asc'=>'address.province_id ASC',
'desc'=>'address.province_id DESC',
),
)
)
));
}
在我看来,我有
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'applicants-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'first_name',
'last_name',
'phn',
array(
'name'=>'band_id',
'value'=>'$data->band->name',
'filter'=>CHtml::listData(Band::model()->findAll(), 'id', 'name')
),
array(
'name'=>'city',
'value'=>'$data->address->city', // causes 'Trying to get property of non-object' - no error if $data->city
'filter'=>CHtml::listData(Address::model()->findAll(), 'city', 'city'),
),
array(
'class'=>'CButtonColumn',
'template'=>'{view}',
'buttons'=>array(
'view'=>array(
'url'=>'Yii::app()->createUrl("/applicant/view", array("id"=>$data->id))',
)
)
),
)
));
我有城市和下拉列表显示,但当我从下拉列表中选择是城市,它不会过滤具有正确结果的小部件
我在下面几页中尝试了解决方案
是否在比较语句中搜索城市表的主键 例如,如果city的主键是city_id,则需要分别在模型和视图中更改以下行:
$criteria->compare('address.city_id',$this->city);
'filter'=>CHtml::listData(Address::model()->findAll(), 'city_id', 'city')
在本例中,您将保留city作为名称,以便它在网格中显示为文本,但在value属性的listData中指定主键city\u id。否。我只是尝试筛选城市。由于用户可以放置他们想要的任何城市,因此没有城市的查找表,因此我认为下拉列表是用户输入的城市列表。然后,我将使用Firebug或类似工具来确保传递了正确的数据,并且没有JS错误。如果城市名称进入列表ok,那么您的关系在那里看起来是ok的,但是根据php注释,不清楚您是否仍然收到错误。您还可以打开查询日志记录或使用MySql日志来了解查询试图执行的操作。此外,如果城市字段有时为空,您可以看到:有关如何构造值。一些城市字段可以为空。下次我再做firebug的时候,我会看看它的。谢谢你的帮助。