Dataprovider For Related Activerecord Models

Is there an easy way to construct a dataprovider that returns objects related to another object?

I’m basically looking for the simplest version of this function:


 * @param CActiveRecord $model        The primary model whose relations the provider will retrieve

 * @param string        $relationName The name of the relation we want to retrieve

 * @return CActiveDataProvider Provider that returns the related models


public function getRelationProvider($model, $relationName);

I do not want to return an array of related objects. Some instances may have thousands of related objects and I’m concerned about performance issues. (Sidenote: is that a valid concern? Should I just use CArrayDataProvider?)

I have considered this solution but it seems somewhat complicated:


[*]Parse the relation declaration into a criteria object

[*]Merge that criteria with the DbCriteria of an instance of the related model

[*]Construct a dataprovider using the updated instance


I’d like to avoid that if possible. Any ideas/patterns for solving this problem?

Also looking for this. Please let me now if you come up with any realisation.

I have another realisation idea.

Make AdapterForRelationDataprovider with "model" property.

Set AdapterForRelationDataprovider->model=your model;

Set AdapterForRelationDataprovider->relationName= name of relation which you need

Set AdapterForRelationDataprovider->relationParams= base params for filtering relations

Override findAll() method, so it could call your model->getRelated(…) with limit in params (add offset and limit to base params via CMap::merge).


Hi there,

I’m curious about what use case you have in your mind.

Is it something that you can not do with the data provider of the related model itself?

Yes, I’m making it for general case.

I have controller for viewing any model, by pk, or list of pks, or scope, or list of scopes, or by relation from another model, etc.

For example if I click "related orderedProducts" I get list of 17 related items (depending on scenario and context view of model(s) can be modified).

There many models with many different relations + there will be new models in future, I don’t want to make changes every time. So I need realisation for general case.