Selecting From Multiple Tables With Cactivedataprovider

I have 3 tables:

Topics, Users and Details

(those are some of the tables of a custom forum)

Topics contains (among other usual fields) the id (FK) of the user that created the topic. Users contains nick/pass and id (PK) Details contains (among other usual fields) the id (FK) of the user.

Relations: One user can have one 1 detail. One user can have multiple topics, but a topic can be created only by one user.

Topic relations:

return array(

'user' => array(self::BELONGS_TO, 'User', 'User_iduser'),


User relations:

return array(

    'details' => array(self::HAS_ONE, 'Details', 'User_iduser'),


I’m trying to get a list with Topics and User details (let’s say for example the topic name and the user’s name).

Currently I have this:

$dataProvider=new CActiveDataProvider('Topic', array(





But as you can imagine, it’s not working (read as in it’s not selecting anything from the tbles Users or Details).

What’s wrong with my code?


This code selects fields from the table user (and the topic table):


But this one won’t select from details, users and topic:


Also, I need a


solution as I need it for zii widgets (means, even if some kind of modification on the


code get’s to select from the 3 tables it won’t be really helpful)

in grid view or list view try this

          array(            // display 'author.username' using an expression




I cant really understand what’s your point actually… :lol:

Thing is ¿how should I create the adapter to retrieve data from those 3 tables at the same time?

I’m having the same trouble, if someone can help us, it will be very appreciated.

Hi EzeTejada

Welcome on your first post :) Try the solution from this thread.

In the Topic model:

public function relations()


        return array(


         'details'=>array(self::HAS_ONE,'Detail', '','on'=>'User_idtopic=User_iddetail'),




and in the Detail model

public function relations()


        return array(


         'topics'=>array(self::HAS_MANY,'Topic', '','on'=>'User_iddetail=User_iduser'),




Then you can call the details using with(‘details’);