问题
Basically I would like to recreate this in CakePHP:
Many to many relationships with additional data on the relationship
or in other words, my intermediary table to have extra fields defining the type of relationship.
How do I set this up in CakePHP?
You can use as example:
model company:
id
name
person_id
model person:
id
name
model company_person:
company_id
person_id
position
thanx
回答1:
if you want to have additional data in HABTM, use 2 hasMany relationships instead. So if A HABTM B, then set it up as: A hasMany A_B, B hasMany A_B, A_B belongsTo A, A_B belongsTo B. AFAIK, Cake support for HABTM is not very flexible.
回答2:
class Person extends AppModel {
...
$hasAndBelongsToMany = array('Company', array('with' => 'CompanyPerson'));
....
}
来源:https://stackoverflow.com/questions/6896481/cakephp-defining-additional-fields-in-habtm-manytomany-relationship