0
Votes
Solved!
How to display 2 related models as one in ex. CGridView?
CGridViewmergemultiple models as one
Hi
I have 2 models (tables) which are to be related.
- actionPlan(id, hp_id, up_id, text)
- yearRapport(id, hp_id, up_id, comment)
actionPlan(id, hp_id, up_id, text) yearRapport(id, hp_id, up_id, comment)
As you can see they are related by keys (id, hp_id, up_id).
Now the question is how to write the relation in model to be able to show “text” row and “comment” row which belongs to it in a CGridView?
Thanks a lot!
Possibly related Questions
-
CJuiDialog: referencing attribute
January 23, 2012 By msimaremare 2 Answers
TAGS
actioncreate ( x 2 )ACTIVERECORD ( x 4 )AJAX ( x 10 )ajaxSubmitButton ( x 4 )AR ( x 2 )cactivedataprovider ( x 2 )CActiveForm ( x 2 )cactiverecord ( x 2 )CGridView ( x 14 )CJuiAutoComplete ( x 3 )CJuiDialog ( x 8 )CListView ( x 8 )controller ( x 7 )Css ( x 2 )CStarRating ( x 2 )CTreeView ( x 2 )database ( x 5 )dialog ( x 2 )dropdownlist ( x 3 )EGMaps ( x 3 )extension ( x 3 )file ( x 2 )form ( x 6 )Giix ( x 2 )gridview ( x 2 )html ( x 2 )javascript ( x 3 )many_many ( x 4 )merge ( x 2 )model ( x 4 )Modules ( x 3 )multilanguage ( x 2 )php ( x 2 )redirect ( x 4 )Registration ( x 3 )relations ( x 5 )renderPartial ( x 8 )session ( x 2 )sorting ( x 3 )sql ( x 4 )url ( x 3 )urlManager ( x 3 )validation ( x 3 )yii ( x 11 )YiiAnswers Site ( x 4 )This website counts Questions and 429 Answers by 7955 registered users.

Assuming that (id, hp_id, and up_id) are unique, I would proceed something like this..
(CODE NOT TESTED)
in actionplan model
public function relations() { // NOTE: you may need to adjust the relation name and the related // class name for the relations automatically generated below. return array( 'myrelationship' => array(self::HAS_ONE, 'yearRapport', 'id, hp_id, and up_id'), ); }in controller
$dataProvider=new CActiveDataProvider('actionPlan', array('pagination'=>array( 'pageSize'=>3, ),));in view
$this->widget('zii.widgets.grid.CGridView', array( 'dataProvider'=>$dataProvider, 'columns'=>array( 'text', // display the 'text' attribute 'myrelationship.comment', // display the 'comment' attribute of the 'myrelationship' relation ), ));Helpful links
http://tipstank.com/2010/08/30/yii-composite-primary-key-reference-composite-foreign-key-example/
http://www.yiiframework.com/doc/guide/1.1/en/database.arr
http://www.yiiframework.com/doc/api/1.1/CGridView
Hope this helps
By tuolden · 600 days 23 hours 56 mins ago
Questions: 25 Accepted: 0 ( 0% ) | Reputation: 0
great one tuolden
By tonydspaniard · 600 days 21 hours 5 mins ago
I trying doing it like you described and it still doesn’t work.
Still ‘myrelationship.comment’ shows nothing, but I also noticed that if I change the field to non-existing like ‘myrelationship.test’ there is no change or error/exception. Weird..
Why is that?
$this->widget('zii.widgets.grid.CGridView', array( 'dataProvider'=>$dataProvider, 'columns'=>array( 'text', // display the 'text' attribute 'myrelationship.comment', // display the 'comment' attribute of the 'myrelationship' relation ), ));By xatep · 600 days 19 hours 37 mins ago
Questions: 2 Accepted: 0 ( 0% ) | Reputation: 0
What are the outputs?
Can you view the “text” field?
Do you have the correct data in DB?
actionPlan.id = yearRapport.id
actionPlan.hp_id= yearRapport.hp_id
actionPlan.up_id= yearRapport.up_id
Try also using
Another useful link for debugging
By tuolden · 600 days 18 hours 39 mins ago
Questions: 25 Accepted: 0 ( 0% ) | Reputation: 0
Solved on Stack Overflow
http://stackoverflow.com/questions/7604541/yii-relations-trouble-when-trying-to-display-cgridview-from-2-models
Problem was in the database as I didn’t refer from foreign key in first table to primary key in the second.
By xatep · 599 days 23 hours 32 mins ago
Questions: 2 Accepted: 0 ( 0% ) | Reputation: 0