Relating 3 models
#1
Posted 05 January 2012 - 06:17 AM
#2
Posted 05 January 2012 - 06:29 AM
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
#3
Posted 05 January 2012 - 06:36 AM
tri, on 05 January 2012 - 06:29 AM, said:
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.
#4
Posted 05 January 2012 - 07:15 AM
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
#5
Posted 05 January 2012 - 07:22 AM
tri, on 05 January 2012 - 07:15 AM, said:
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.
#6
Posted 05 January 2012 - 07:27 AM
#7
Posted 06 January 2012 - 01:38 AM
tri, on 05 January 2012 - 07:27 AM, said:
/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
#8
Posted 06 January 2012 - 04:02 PM
/Tommy
#9
Posted 09 January 2012 - 01:02 AM
tri, on 06 January 2012 - 04:02 PM, said:
/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.
#10
Posted 09 January 2012 - 04:32 AM
Praveen, on 09 January 2012 - 01:02 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
#11
Posted 09 January 2012 - 05:11 AM
tri, on 09 January 2012 - 04:32 AM, said:
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..

Help












