Eagerly fetch related objects after fetching model?

Hello all!

Is there any way of eagerly fetching all objects through a relation when you already have the AR object?

I.e. I have a Event object that contains multiple participants, each with multiple exchanges. I also use the CTabView widget. In one of the tabbed panels, I need to access all of the exchanges of all the people in a given event. If I do:

foreach ( $model -> participants as $participant )

  foreach ( $participant -> exchanges as $exchange )

    echo $exchange -> amount;

Yii will generate one query for each person because of the lazy loading. Can I somehow tell the model AFTER fetching it to eagerly load all participants and their exchanges?

If not, this would be a great additions to the next version of Yii!



Yes, but I don't want to load all participants and all exchanges for all the different tabs… The controller currently only loads the event itself, and passes the Event model to the view. Inside the view, the CTabView widget then passes that model onwards to the partial views that represent the different tab panels.

I could fetch the model again inside the partial view, but that seems unnecessary… Should be some way of loading data eagerly on a already fetched model…


Beautiful! Thank you =D