AR relations, which "base model"

I can use AR relations this way successfully:

[tt]

                          ___ belongs to ___ C

                          |

A — belongs to — B —|___ belongs to ___ D

[/tt]

(pseudo-code)



// common relations, no problem


'BtoC'=>array(self::BELONGS_TO, 'C', 'FK(C)'),


'BtoD'=>array(self::BELONGS_TO, 'D', 'FK(D)'),





// A to B relation, without and with condition


'AtoB'=>array(self::BELONGS_TO, 'B', 'FK(B)'),


'AtoB'=>array(self::BELONGS_TO, 'B', 'FK(B)', 'condition'=>'t3.Username="'.Yii::app()->user->name.'"'),





// call


A::model()->with(array('AtoB'=>array('C', 'D')))->findAll();


But if I try the other way around I get the "invalid foreign key" message ("The foreign key does not point to either joining table").

I also tried the Many-Many syntax

[tt]

      ___ belongs to ___ C

    |

B —|___ belongs to ___ D

    |

    |___ has many ___ A

[/tt]



// common relations, no problem


'BtoC'=>array(self::BELONGS_TO, 'C', 'FK(C)'),


'BtoD'=>array(self::BELONGS_TO, 'D', 'FK(D)'),





// B to A relation, I tried these two ways


'BtoA'=>array(self::HAS_MANY, 'A', 'FK(A)'),


'BtoA'=>array(self::HAS_MANY, 'A', 'A(AtoB)'),





// call


B::model()->with(array('BtoA', 'C', 'D'))->findAll();


What am I doing wrong here?

Also, in the first example, why did I have to replace ??.Username with t3.Username?

/Tommy

Edit: PK —> FK

The 3rd parameter should be FK, not PK.

Yes, of course. I edited my message. I recall from reading framework code, the DB constraint name isn't used, only the table name and FK column.

Gave it another try and didn't get the error message this time. I have look into this a bit more. Still see only the small number of records from the B table, though. Maybe the first way is the best way to do it, even if the view will need more changes?

Now it seems to work, even the '??' aliasToken. The latter might have worked before, initially i tried to pass it with the findAll call.

I realized that the generated, then extended, B list view need to have an additional inner foreach loop, iterating over A.

/Tommy

Edit: I use r368 and don't remember exactly which change made the error message go away (occured when I changed back from the attempted "many-many nested table"  syntax).