Multi-column foreign keys and AR relations?

Hi there.

I was twiddling around a bit and didn’t find a way to define multiple column foreign keys to work as a Yii relation.

Is it even possible? I find no references anywhere.

So, simplifiedly like the following postgresql syntax:




CREATE TABLE A(

   A_id INTEGER NOT NULL,

   PRIMARY KEY(A_id)

);


CREATE TABLE AA(

   AA_id INTEGER NOT NULL,

   PRIMARY KEY(AA_id)

)


CREATE TABLE B(

   A_id INTEGER NOT NULL REFERENCES A(A_id),

   AA_id INTEGER NOT NULL REFERENCES AA(AA_id),

   PRIMARY KEY(A_id, AA_id)

);


CREATE TABLE ProblemTable(

   A_id INTEGER NOT NULL,

   AA_id INTEGER NOT NULL,

   FOREIGN KEY(A_id, AA_id) REFERENCES B

   PRIMARY KEY(A_id, AA_id)

);



This is simplified, but the idea anyhows is to go around having to make unnecessary id fields.

The SQL works as such, but there’s no way of defining a relation in Yii for ProblemTable.

Assuming we’d have also the A, AA, B and ProblemTable AR objects:

B::relations():




...


'r_ProblemTable'=>array(self::MANY_MANY,'ProblemTable','ProblemTable(A_id,AA_id)'),

...



-> Results in an error trying to tell me that the joining table should contain foreign keys from both joined tables.

B::relations():




'r_ProblemTable'=>array(self::HAS_MANY,'ProblemTable','A_id'),



-> Well, doesn’t work anyways, as A_id is not unique among ProblemTable rows.




'r_ProblemTable'=>array(self::HAS_MANY,'ProblemTable',array('A_id','AA_id')),



-> Umm, I guess this isn’t even correct syntax? :)

So, is there a Yii-ish way to do this?

Using Yii 1.0.11. Thanks.

As stated in API, "for composite foreign keys, they must be listed together, separating with space or comma".

i seem to have a similar problem on my thread:

http://www.yiiframework.com/forum/index.php?/topic/25229-help-in-relations/

though i have no foreign keys defined in my database tables