Register here!

Forgot password?

ASK NEW QUESTION

Home Page » Models » Relations » How to display 2 related models as one in ex. CGridView?
0 Votes
Vote Con!
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.

  1. actionPlan(id, hp_id, up_id, text)
  2.  
  3. 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!

By xatep in Relations · Asked 601 days 4 hours ago

Questions: 2 Accepted: 0 ( 0% ) | Reputation: 0

 

Answers (5)

  1. 0 votes

    Assuming that (id, hp_id, and up_id) are unique, I would proceed something like this..

    (CODE NOT TESTED)

    in actionplan model

    1.     public function relations()
    2.     {
    3.         // NOTE: you may need to adjust the relation name and the related
    4.         // class name for the relations automatically generated below.
    5.         return array(
    6.             'myrelationship' => array(self::HAS_ONE, 'yearRapport', 'id, hp_id, and up_id'),
    7.         );
    8.     }
    	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

    1.         $dataProvider=new CActiveDataProvider('actionPlan', array('pagination'=>array(
    2.                 'pageSize'=>3,
    3.             ),));
    		$dataProvider=new CActiveDataProvider('actionPlan', array('pagination'=>array(
    				'pageSize'=>3,
    			),));

    in view

    1. $this->widget('zii.widgets.grid.CGridView', array(
    2.     'dataProvider'=>$dataProvider,
    3.     'columns'=>array(
    4.         'text',          // display the 'text' attribute
    5.         'myrelationship.comment',  // display the 'comment' attribute of the 'myrelationship' relation
    6.     ),
    7. ));
    $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

  2. 0 votes

    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?

    1.     $this->widget('zii.widgets.grid.CGridView', array(
    2.         'dataProvider'=>$dataProvider,
    3.         'columns'=>array(
    4.             'text',          // display the 'text' attribute
    5.             'myrelationship.comment',  // display the 'comment' attribute of the 'myrelationship' relation
    6.         ),
    7.     ));
        $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

  3. 0 votes

    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

    1. echo Yii::trace(CVarDumper::dumpAsString($dataProvider),'vardump');
    echo Yii::trace(CVarDumper::dumpAsString($dataProvider),'vardump');

    Another useful link for debugging

    By tuolden · 600 days 18 hours 39 mins ago

    Questions: 25 Accepted: 0 ( 0% ) | Reputation: 0

  4. 0 votes
    Accepted

    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

© YiiAnswers.com 2011. All rights reserved.

Switch to our mobile site