Relating 3 models

Hi, I got 3 tables , table A with a primary key, Table B with a primary key. table C Linking table A and Table B primary key… How i relate this in relation ?

Read more about Relational AR in the guide.

If still without a clue, be more specific about the table structure and your use case.

Hint: If the forum search doesn’t return anything, use google “site:yiiframework.com your-search-words”

/Tommy

Hi Tommy,

Have gone thru that. but could not implement the same.

Table User

Id - PK

Name

Table Group

Id - PK

Name

Table User_Group

Id- PK

user_id - FK - Table A

group_id - FK - Table B

Now my Relation will be

‘User_Group’ => array(self::HAS_ONE, ‘user_Group’, ‘user_id’, ‘joinType’=>‘INNER JOIN’),

How to get the the group name on loadmodel.

Thanks.

This looks to me like a MANY_MANY relationship.

If you declared relationships in the database (assuming mysql/innodb) and subsequently generated crud, you should be able to lazy load and access using something like $user->groups->Name.

Here’s an extension you may want to check out

/Tommy

Hi,

I have used mysql innodb and defined all foreign keys and primary as u mentioned. And created crud using Gii.

Its showing Property "User.groups" is not defined.

Thanks.

Take a closer look at the relations() method in User.php. It might be something like "user_group".

/Tommy

Hi,

Nope that doesnt seem like an issue, I tried the following,

$modelUser = $this->loadModel($id); in view action and passed it on to render action

$this->render(‘view’, array(

        'modelUser' => $modelUser,


    ));

in view when i try to <?php print_r($modelUser); ?> it dislpays all the info,

but when i try to do <?php print_r($modelUser->user_group); ?> it shows error.

I think i need to create a mysql view joining 3 tables and pass on the view table to model to display information…

Regards,

Praveen

Please look up (in User.php method relations) the actual name of the expected Gii created relationship, as I suggested.

/Tommy

Thanks for your time, but that doesnt seem to be a problem. Sorry m a newbie, may i know

  1. Does Yii support views

  2. Do i need to define a relation like

‘User_Group’ => array(self::HAS_ONE, ‘user_Group’, ‘user_id’, ‘joinType’=>‘INNER JOIN’),

‘group’ => array(self::HAS_ONE, ‘Group’, user_group(‘id’)),

Let me know if i am wrong.

Thank You.

Since you don’t answer my question, and seemingly are reluctant to establishing a point of reference with regard to existing code and db structure, let’s wait for somebody else to help you out. Possibly you will have to elaborate a bit about your use case. I’ll rest my case.

BTW As far as I know, some people use db views. You should be able to find existing threads discussing that topic.

/Tommy

Hi Tri,

Sorry for that, due to the rules and regulations of my firm, i cant share any code or db structure. Right now i created a view and assigned a primary key and gridView is working fine. thanks for your help… will keep you posted if anything else comes up… :) :)

Thanks…