user5853948
user5853948

Reputation:

Linking two tables with two (composite) foreign key relation

Relations

Two columns from table B are associated with two columns from table A.

Cookbook

No example found in the cookbook. http://book.cakephp.org/3.0/en/orm/associations.html

How can I specify these relations? Already tried this:

$this->hasMany('B', [
        'conditions' => ['A.a_id' => 'B.a_id', 'A.a_name' => 'B.a_name']
    ]);

And this:

$this->hasMany('B1', [
        'foreignKey' => 'a_id',
        'joinType' => 'INNER',
        'className' => 'B'
    ]);

$this->hasMany('B2', [
        'foreignKey' => 'a_name',
        'joinType' => 'INNER',
        'className' => 'B'
    ]);

Upvotes: 2

Views: 1542

Answers (1)

ndm
ndm

Reputation: 60473

Composite keys can be specified using arrays, this is supported for foreign keys as well as primary keys nearly everywhere.

$this->hasMany('B', [
    'foreignKey' => [
        'a_id',
        'a_name'
    ],
    'bindingKey' => [
        'a_id',
        'a_name'
    ]
]);

An example in the docs wouldn't hurt, you may want to open a ticket over at GitHub.

ps. hasMany associations do not support a joinType option.

Upvotes: 4

Related Questions