[solved] how to create relation many_many between 3 foreign key

according to Relational Active Record, Yii has provided for relation MANY_MANY with 2 foreign key.

i want to know, is there any solution to use MANY_MANY with 3 foreign key?

for example:

table_a


a_id (PK)

a_field

table_b


b_id (PK)

b_field

table_c


c_id (PK)

c_field

table_abc (relation between table_a, table_b & table_c)


a_id (FK)

b_id (FK)

c_id (FK)

how can i create an relation between 4 table above?

please advice…

thanks

I’m fairly certain Yii doesn’t provide a solution for a table with 3 FKs (and I don’t even know what it would look like – aside from very intriguing!)

What you could do of course is regard table_abc as multiple MANY_MANY tables in one, meaning you can have

a MANY_MANY b

a MANY_MANY c

b MANY_MANY a

b MANY_MANY c

c MANY_MANY a

c MANY_MANY b

Although this sounds rather tricky to manage. If there is a way to solve your problem using a default database structure I’d seriously consider using it; this sounds like a major headache waiting to happen.

I’d recommend something like this:

a HAS_MANY b through abc

b HAS_MANY a through abc

a HAS_MANY c through abc

etc…

but the actual implementation highly depends on your Domain Model.

thanks a lot for your suggestion.

it’s very helpful… :)

i used both of the suggestion.

i used Many_Many, in case if i don’t want to get any field in relation.

i used Has_Many through, in case if i want to get some filed from relation.