In CakePHP, how do you setup a zero-to-many relationship?
For example, I have three models: Player, Pitch开发者_开发百科erStats, and FielderStats. A Player is either a Pitcher or a Fielder. Accordingly, the Player will have (zero or many) Pitcher stats or (zero or many) Fielder stats. The PitcherStats will have a belongsTo relationship to Player. And so will the FielderStats. How does the Player model relate to the other two?
you should have one table for stats with foreign keys to the concrete stats. so you should build 3 tables for stats, one which will hold all stats (foreign key and type) and two will hold the concrete values.
zero to x relation ship is a subgroup of 1/n to x relationships
there are in fact only 2:
- 1:n
- n:m
in different combinations and with different choices of foreign keys
e.g. 1:1 is nothing but 1:n where you can decide where to put the key (left or right table).
0:x is a simple case of 1:n or n:m where there is no record yet.
精彩评论