ehcg
(Sterlingl)
March 26, 2011, 6:55am
1
I was wondering about how to set up a relational active record in a situation where the link could be made based on two or more foreign keys.
Example:
Users
user_id int
name char
Challenges
–
challenger_id int
opponent_id int
others
I want User to have a relation called "challenges" which links to all challenges where the user id matches either challenger_id or opponent_id.
I currently have
'challenges'=>array(self::HAS_MANY, 'Challenge', 'challenger_id'),
'challenged'=>array(self::HAS_MANY, 'Challenge', 'opponent_id')
but I would like to have one relation which combines both of these.
Is this possible?
Command
(Command)
March 26, 2011, 7:22am
2
something like
Challenge::model()->with('challenges','challenged
')->findAll();
if i understand what you want correctly
Seal
(Sealtrip)
March 26, 2011, 8:58am
3
[color="#808080 "][color="gray"][size="2"]Interesting![/size][/color][/color]
[color="#808080 "][color="gray"][size="2"]@Command , I believe ehcg wants to declare the relationship [/size][/color][/color][color="#808080 "][size="2"]relations() [/size][/color][color="#808080 "][size="2"]within the model class. [/size][/color][color="#808080 "][size="2"]From what he has shown us, my understanding is that there may be an additional table called opponents.[/size][/color]
[color="#808080 "][size="2"]@ehcg is there an additional opponent table? [/size][/color]
[size="2"][color="#808080 "]Can you try something like below? [/color][/size]
[size=“2”][color="#008800 "]‘challenges’[/color][color="#666600 "]=>[/color][color="#000000 "]array[/color][color="#666600 "]([/color][color="#000088 "]self[/color][color="#666600 "]::[/color][color="#000000 "]HAS_MANY[/color][color="#666600 "],[/color] [color="#008800 "]‘Challenge’[/color][color="#666600 "],[/color] [color="#008800 "]’[/color][color="#008800 "]Challenges([/color][color="#008800 "]challenger_id,[/color][color="#008800 "]opponent_id[/color][color="#008800 "])’[/color][color="#666600 "]),[/color][/size]
[color="#808080 "][size="2"]Let us know the outcome? [/size][/color]
ehcg
(Sterlingl)
March 26, 2011, 9:15am
4
opponent_id is actually a foreign key that also refers to the User table. A user can either be the challenger or the opponent for a challenge.
Seal:
[color="#808080 "][color="gray"][size="2"]Interesting![/size][/color][/color]
[color="#808080 "][color="gray"][size="2"]@Command , I believe ehcg wants to declare the relationship [/size][/color][/color][color="#808080 "][size="2"]relations() [/size][/color][color="#808080 "][size="2"]within the model class. [/size][/color][color="#808080 "][size="2"]From what he has shown us, my understanding is that there may be an additional table called opponents.[/size][/color]
[color="#808080 "][size="2"]@ehcg is there an additional opponent table? [/size][/color]
[size="2"][color="#808080 "]Can you try something like below? [/color][/size]
[size=“2”][color="#008800 "]‘challenges’[/color][color="#666600 "]=>[/color][color="#000000 "]array[/color][color="#666600 "]([/color][color="#000088 "]self[/color][color="#666600 "]::[/color][color="#000000 "]HAS_MANY[/color][color="#666600 "],[/color] [color="#008800 "]‘Challenge’[/color][color="#666600 "],[/color] [color="#008800 "]’[/color][color="#008800 "]Challenges([/color][color="#008800 "]challenger_id,[/color][color="#008800 "]opponent_id[/color][color="#008800 "])’[/color][color="#666600 "]),[/color][/size]
[color="#808080 "][size="2"]Let us know the outcome? [/size][/color]