mgPePe
mgPePe

Reputation: 5907

CakePHP defining additional fields in HABTM/ManyToMany relationship?

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

Upvotes: 2

Views: 2539

Answers (2)

Anh Pham
Anh Pham

Reputation: 5481

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.

Upvotes: 6

ksn
ksn

Reputation: 633

class Person extends AppModel {
    ...
    $hasAndBelongsToMany = array('Company', array('with' => 'CompanyPerson'));
    ....
}

Upvotes: 1

Related Questions