/**
* Returns a model used to populate a filterable, searchable
* and sortable CGridView with the records found by a model relation.
*
* Usage:
* $relatedSearchModel = $model->getRelatedSearchModel('relationName');
*
* Then, when invoking CGridView:
* ...
* 'dataProvider' => $relatedSearchModel->search(),
* 'filter' => $relatedSearchModel,
* ...
* @returns CActiveRecord
*/
public function getRelatedSearchModel($name)
{
$md = $this->getMetaData();
if (!isset($md->relations[$name])) {
throw new CDbException(Yii::t('yii', '{class} does not have relation "{name}".', array('{class}' => get_class($this), '{name}' => $name)));
}
$relation = $md->relations[$name];
if (!($relation instanceof CHasManyRelation)) {
throw new CException("Currently works with HAS_MANY relations only");
}
$className = $relation->className;
$related = new $className('search');
$related->unsetAttributes();
$related->{$relation->foreignKey} = $this->primaryKey;
if (isset($_GET[$className])) {
$related->attributes = $_GET[$className];
}
return $related;
}
Branch "back-to-the-roots"