Yii Framework Forum: Relating 3 models - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Relating 3 models Rate Topic: -----

#1 User is offline   Praveen J 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 29-December 11

Posted 05 January 2012 - 06:17 AM

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 ?
0

#2 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,628
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 05 January 2012 - 06:29 AM

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

This post has been edited by tri: 05 January 2012 - 06:31 AM

Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#3 User is offline   Praveen J 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 29-December 11

Posted 05 January 2012 - 06:36 AM

View Posttri, on 05 January 2012 - 06:29 AM, said:

Read more about Relational AR in the guide.

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

/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.
0

#4 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,628
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 05 January 2012 - 07:15 AM

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

This post has been edited by tri: 05 January 2012 - 07:18 AM

Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#5 User is offline   Praveen J 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 29-December 11

Posted 05 January 2012 - 07:22 AM

View Posttri, on 05 January 2012 - 07:15 AM, said:

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.
0

#6 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,628
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 05 January 2012 - 07:27 AM

View PostPraveen, on 05 January 2012 - 07:22 AM, said:

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


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

/Tommy
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#7 User is offline   Praveen J 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 29-December 11

Posted 06 January 2012 - 01:38 AM

View Posttri, on 05 January 2012 - 07:27 AM, said:

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
0

#8 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,628
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 06 January 2012 - 04:02 PM

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

/Tommy
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#9 User is offline   Praveen J 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 29-December 11

Posted 09 January 2012 - 01:02 AM

View Posttri, on 06 January 2012 - 04:02 PM, said:

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.
0

#10 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,628
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 09 January 2012 - 04:32 AM

View PostPraveen, on 09 January 2012 - 01:02 AM, said:

Thanks for your time, but that doesnt seem to be a problem.


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
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#11 User is offline   Praveen J 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 29-December 11

Posted 09 January 2012 - 05:11 AM

View Posttri, on 09 January 2012 - 04:32 AM, said:

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..
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users