Yii CGridView,我无法使用 HAS_MANY 关系显示来自其他模型的字段
Yii CGridView, I can't display fields form other model using HAS_MANY relationship
我在 2 个模型之间有一个 HAS_MANY 关系,我使用 bookID 作为外键
模型 1 - Importedbooks,Importedbooks 可以有多个 CountryOfImport
public function relations()
{
return array(
'CountryOfImportObj'=>array(self::HAS_MANY, 'CountryOfImport', 'bookID')
);
}
模型 2 CountryOfImport,CountryOfImport 属于 Importedbooks
public function relations()
{
return array(
'ImportBooksObj'=>array(self::BELONGS_TO, 'Importedbooks', 'bookID')
);
}
现在,对于我的 CGridView,我使用 Importedbooks 模型的模型->search() 作为我的数据提供者,从这里开始我被卡住了。
CGridView
$data = $model->search();
$data->setPagination(array('pageSize'=>'5'));
$data->criteria->addCondition('active = "yes"');
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$data
,'filter'=>$model
,'pager'=>array('header'=>'')
,'columns'=>array(
'id',
'bookYear',
'bookTitle',
'DISPLAY VALUE FROM OTHER model HERE'
)
)
);
导入书籍模型的数据提供者,我将其用作网格的数据提供者
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('bookID',$this->bookID);
$criteria->compare('countryName',$this->countryName,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
这种关系有效,因为我可以在我的控制器中使用下面的代码从另一个模型中获取字段(国家/地区名称)
$model = Importedbooks::model()->with('CountryOfImportObj')->findbyPK(3);
print_r($model->CountryOfImportObj[0]->countryName);
例如 Importedbooks 有很多 CountryOfImport,在一行中您必须显示或所有国家名称或具体名称取决于 ID 或 countryName 或其他一些属性。
您可以将匿名函数作为值传递:
'columns' => array(
'id',
'bookYear',
'bookTitle',
array(
'header' => 'Countries',
'type' => 'raw',
// if you have defined counrtyName attribute in 'Importedbooks' model, if not you can remove this row
// also CGridView 'uses' attribute 'name' for filtering or sorting
'name' => 'countryName',
'value' => function($data, $row) { //$data is item of DataProvider, $row is number of row (starts from 0)
$countries = CHtml::listData((array)$data->CountryOfImportObj, 'id', 'countryName');
return implode(', ', $countries);
}
)
)
如果您也想按外部属性进行筛选或排序,则必须“declare them”
谢谢!
我在 2 个模型之间有一个 HAS_MANY 关系,我使用 bookID 作为外键
模型 1 - Importedbooks,Importedbooks 可以有多个 CountryOfImport
public function relations()
{
return array(
'CountryOfImportObj'=>array(self::HAS_MANY, 'CountryOfImport', 'bookID')
);
}
模型 2 CountryOfImport,CountryOfImport 属于 Importedbooks
public function relations()
{
return array(
'ImportBooksObj'=>array(self::BELONGS_TO, 'Importedbooks', 'bookID')
);
}
现在,对于我的 CGridView,我使用 Importedbooks 模型的模型->search() 作为我的数据提供者,从这里开始我被卡住了。
CGridView
$data = $model->search();
$data->setPagination(array('pageSize'=>'5'));
$data->criteria->addCondition('active = "yes"');
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$data
,'filter'=>$model
,'pager'=>array('header'=>'')
,'columns'=>array(
'id',
'bookYear',
'bookTitle',
'DISPLAY VALUE FROM OTHER model HERE'
)
)
);
导入书籍模型的数据提供者,我将其用作网格的数据提供者
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('bookID',$this->bookID);
$criteria->compare('countryName',$this->countryName,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
这种关系有效,因为我可以在我的控制器中使用下面的代码从另一个模型中获取字段(国家/地区名称)
$model = Importedbooks::model()->with('CountryOfImportObj')->findbyPK(3);
print_r($model->CountryOfImportObj[0]->countryName);
例如 Importedbooks 有很多 CountryOfImport,在一行中您必须显示或所有国家名称或具体名称取决于 ID 或 countryName 或其他一些属性。
您可以将匿名函数作为值传递:
'columns' => array(
'id',
'bookYear',
'bookTitle',
array(
'header' => 'Countries',
'type' => 'raw',
// if you have defined counrtyName attribute in 'Importedbooks' model, if not you can remove this row
// also CGridView 'uses' attribute 'name' for filtering or sorting
'name' => 'countryName',
'value' => function($data, $row) { //$data is item of DataProvider, $row is number of row (starts from 0)
$countries = CHtml::listData((array)$data->CountryOfImportObj, 'id', 'countryName');
return implode(', ', $countries);
}
)
)
如果您也想按外部属性进行筛选或排序,则必须“declare them”
谢谢!