display join queries in CgridView

Hi everyone,

Im using the CgridView widget to display results from a join querie.

In my controller i have this code:




$criteria->select = 't.*';

$criteria->join ='INNER JOIN user_to_post ON user_to_post.user_id = t.id';

In the user model i have this:




'posts' => array(self::MANY_MANY, 'Post','user_to_post(user_id,post_id)'),

In the CgridView, i want to display a user with his posts:

ex:


 User     Post

          user1    post1

          user1    post2

          user1    post3

     

So i tried something like this in my view:


'columns'=>array(

	array(

         'name' => 'Post',

         'value' => '$data->posts[<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />]->title',

        ),

        array(

          'name' => 'user',

          'value' => '$data->username',

        ),



My question is how to pass the right index to $data->posts[] to get the associated posts for a user. For example, $data->posts[0]->title, will display post1 tree times

I hope you understand my question

Thanks

Perhaps use $row.

Read more here:

http://www.yiiframework.com/doc/api/CDataColumn#value-detail

/Tommy

Hi Tommy and thanks for your response,

I already tried with $row, but it works only when i want to list all the post for a user.

Let say i want to retrieve the post 2 of the user 1, i get the post 1 as a result, because the $row contain 0

Any other idea ?

Thanks

I’d say: you can’t. A row in a gridview is related to exactly 1 result row from a query (in your case: a user). You want to create additional rows for each child object (posts). That doesn’t work with CGridView. So either merge all posts somehow and show them in another column or use something else but a CGridView.

I would say your best option is to create a mysql view out of your join query and then let Gii generate the model out of it. Then use that model for your cgridview. I was pulling my hair with cgridview and relations until I understood that solution to most all of my problems was creating a view.