可以在CakePHP中表示复合主键吗?

时间:2014-06-29 14:56:20

标签: mysql cakephp

我有一个像这样的表结构

+--------------+  +------------+  +--------------+  +-----------------+
| ships        |  | ship_types |  | ship_slots   |  | ship_components |
+--------------+  +------------+  +--------------+  +-----------------+
| id           |  | id         |  | id           |  | ship_id         |
| user_id      |  +------------+  | ship_type_id |  | ship_slot_id    |
| ship_type_id |                  +--------------+  +-----------------+
+--------------+  

ship_types hasMany ships
ship_types hasMany ship_slots
ships hasMany ship_components
ship_slots hasMany ship_components

当我做

之类的事情时
ship->find(all, user_id = xyz)

我得到了

[0] =>
    Ship =>
        id 1
    ShipType =>
        ShipSlot =>
            [0] =>
                ShipComponent =>
                    [0] => ship_id 1
                    [1] => ship_id 2
            [1] => ...
[1] =>
    Ship =>
        id 2
    ShipType =>
        ShipSlot =>
            [0] =>
                ShipComponent =>
                    [0] => ship_id 1
                    [1] => ship_id 2
            [1] => ...

我喜欢的是

(ships + ship_slots) hasOne ship_components

ship_id和ship_slot_id构成船舶组件的主键。如何设置模型关联,以便ship_slots仅关联到具有父船的ship_id的ship_components。

select *
from ships
inner join ship_types on ship_types.id = ships.ship_type_id
left join ship_slots on ship_slots.ship_type_id = ship_types.id
left join ship_components on ship_components.ship_id = ships.id and ship_components.ship_slot_id = ship_slots.id
where ships.user_id = 5

0 个答案:

没有答案