softark, on 27 May 2012 - 10:33 AM, said:
Yours is not a question but rather a request about CActiveDataProvider or CArrayDataProvider.
well, I'm not quite sure if that is a feature request or is there some feature I don't know of
Quote
CArrayDataProvider can receive array of model objects, for example $model->posts as rawData. In that case it should be able to tell what model it is dealing with. And CGridView should be able to replace the header labels according to the model.
there are several drawbacks of using CArrayDataProvider with related models array:
1. it does not allow for sorting, filtering etc
2. it doesn't use model's attributeLabels
3. all the related models are obtained at once, they're paginated just before rendering. for huge sets of data it is unnaceptable.
Currently my ActiveRecord class has the following method
/**
* Return the CActiveDataProvider of related models,
* according to relations() definition
* @returns CActiveDataProvider
* @author MichaĆ "migajek" Gajek
*/
public function getRelatedProvider($relation_name)
{
$rels = $this->relations();
if (!is_array($rels) || !isset($rels[$relation_name]))
throw new CException(get_class($this). " has no relation named \"{$relation_name}\" defined!");
$relation = $rels[$relation_name];
if ($relation[0] !== self::HAS_MANY)
throw new CException("getRelatedProvider works with HAS_MANY relations only");
$cdb = new CDbCriteria();
$cdb->compare($relation[2], $this->primaryKey);
return new CActiveDataProvider($relation[1], array(
'criteria' => $cdb,
));
}
which is used as following:
<?php $this->widget('bootstrap.widgets.BootGridView', array(
'dataProvider'=> $model->getRelatedProvider('posts'),
)); ?>
This method should be extended to handle all the relation parameters, currently it just takes the model name and foreign key.
I was wondering if there's any ready to use method in Yii core.
If not, I believe there should be one