CGridView, display nested relations

Hi people,

I’m new to Yii and going crazy on this issue. Any hints would be great, any points there I have to looking for.

Here are my relations:

So, I’m in Profile now and would like to display all App over relations above in a CGridview.

I’ve tried alreay to play with relations, define “through”, use CArrayDataProvider, CActiveDataProvider, but all I’ve got was access to “Device”.

Please dont leave me alone ;)

You need to do as

Create a function in Profile Model and call it in CGrideView


function getCustomerApps(){

if($this->devices){

 foreach($this->devices as $device){

   if($device->apps){

     foreach($device->apps as $app){

     // do what you want

}

}

}

}

}

Please note:

Profile has a relation named devices

Devices has a relation named apps

What about next code?




$Customer = Customer::model->findByPk(CustomerId);

foreach( $Customer->device as $device ){

    foreach( $device->app as $app ){

         echo $app->name?

    }

}



You must have Customer model, and this model have next relation


 Customer -HAS_MANY-> Device -MANY_MANY-> App

@PeRoChAk

@Nikolay I. Belichuk

Many thanks guys, both solution working, I finally got the apps.

I use array_push to merge all app-object in foreach-loop inside the function getCustomerApps and create an


CActiveDtatProvider('App', array('data'=>$model->getCustomerApps()));

and then feed it to CGridView as ‘dataProvider’.

I hope this is way is o.k. to fill the dataProvider.

Thanks again, you made me happy :)