CGridView or custom display

I need to display data on a page from 2 db tables. I have some experience using gridviews in C#/.NET but have not used the yii equivalent. What I need to do, or what I’d like to do, is display data from 2 different tables in one grid type display. This data will be read only, no manipualtion of any kind is necessary. Would CGridview be the best option here, given I’m trying to put data from 2 different tables in one place, or is there something else that might do the trick? Ideas would be greatly appreciated guys, I’m a yii and php novice so be gentle…he he.

Well, if they are in some kind of relation: No problem. If they are entirely unrelated: No idea what you are trying to do, but there is surely a better way :lol:

Well they are related in some way, but it’s a one to many relationship so I can’t put everything in one table. I have 2 tables that I’m trying to display, Poll and Votes. There are many votes to one poll. Can I put data from both in one CGridview?

Can you simply explain what exactly it is that you want? Or is it a big secret? ;)

If you want to list all polls and votes in one GridView: That’s quite possibly. But it won’t look good with a vanilla GridView.

Sorry, here’s roughly what I’m looking for.

Poll table

poll_id

poll_description

poll_result

room_name

before_settings

after_settings

in_session

started

finished

started_by

Vote table:

vote_id

poll_id —> references poll_id above

vote_result

cast_by

I would like to display the following info in one cgridview (or an equivalent type of display) using data from the above tables

poll_description:

poll_result

started

finished

started_by

vote_result

cast_by

So an instance of this display would hold the above information. Bear in mind that every instance, or page of this view would display ONE of each of the first 5 attributes, poll_description thru started_by, and it may hold several vote_results and cast_by’s in this one instance.

Does that make sense?

Yes, it does. And it is all very well possible with the default CGridView. Just be prepared to fill the column’s value property with some complex expressions to iterate over all vote_results and cast_bys.

Thanks for your help, I’ve been looking over the documentation though and the way to do what I need is not obvious to me. Would I be asking too much of you if I said could you put up maybe a small code example of how I’d do this? Thanks regardless for your help.

Certainly. You’ll need to take advatnage of the CDataColumn.value property:




'columns'=>array(

  ...,

  array(

    'header'=>'Vote Results',

    'type'=>'raw',

    'value'=>'foreach($data->votes as $vote) echo "{$vote->vote_result} <br/>"',

  ),

),



maybe it makes more sense to use a ListView?

Hm. Actually … yes, could very well be.