CakePHP defining additional fields in HABTM/ManyToMany relationship?

主宰稳场 提交于 2019-12-10 10:42:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!