Many To Many Relationship And Composite Primary Key

Hi,

I have to tables connected by a third relationship table. However one of the two tables has a composite primary key (see below). How can I declare the MANY_MANY relationship in a model?


CREATE TABLE T1 (

id integer,

someOtherID integer,

somevalue text,

PRIMARY KEY(id, someOtherID)

)


CREATE TABLE T2 (

id integer PRIMARY KEY,

value text

)


CREATE TABLE R (

rID1 integer,

rOtherID1 integer,

rID2 integer REFERENCES T2(id),

FOREIGN KEY (rID1, rOtherID1) REFERENCES T1(id, someOtherID),

PRIMARY KEY (rID1, rOtherID1, rID2)

)

See Working with Databases

  1. Declaring Relationship -> Info: A foreign key may be composite, consisting of two or more columns …

I don’t think it’s possible. Many you can break it down into a HAS_MANY relation and a ‘through’?

Something like this in T1:




public function relations()

{

    return array(

        'r'=>array(self::HAS_MANY, 'R', array('id'=>'rID1', 'someOtherID'=>'rOtherID1')),

        't2'=>array(self::HAS_MANY, 'T2', array('rID2'=>'id'), 'through'=>'r'),

    );

}



and T2 would have something like this:




public function relations()

{

    return array(

        'r'=>array(self::HAS_MANY, 'R', 'rID2'),

        't1'=>array(self::HAS_MANY, 'T1', array('rID1'=>'id', 'rOtherID1'=>'someOtherID'), 'through'=>'r'),

    );

}



I haven’t tested those at all, but it might get you moving in the right direction.