2010-11-11 52 views
0

我有這樣的結構......如何使不同的表樹木CakePHP的

爺爺的hasMany孩子,孩子的hasMany孩子

我可以讓這3代表的樹?我認爲我的問題確實是,我可以只使用這些表而不創建另一個表來構建樹結構嗎?

+1

爲什麼不能讓1人表? 每一行都有它的孩子和父母...... – yvoyer 2010-11-11 18:22:24

回答

2

我不認爲你可以,因爲樹的行爲是針對1個表的。

但是你可以使用表格personspersons_kids

+--------------------+ 
| persons   | 
+--------------------+ 
| id | Name  | 
+--------------------+ 
| 1 | Grandpa | 
+--------------------+ 
| 2 | Dad  | 
+--------------------+ 
| 3 | Me   | 
+--------------------+ 
| 5 | MyChildren | 
+--------------------+ 
| 4 | MyBrother | 
+--------------------+ 

+--------------------+ 
| persons_kids  | 
+--------------------+ 
| parent_id | kid_id | 
+--------------------+ 
| 1  | 2 | // Grandpa -> Dad 
+--------------------+ 
| 2  | 3 | // Dad -> Me 
+--------------------+ 
| 2  | 3 | // Dad -> MyBrother 
+--------------------+ 
| 3  | 5 /// Me -> MyChildren 
+--------------------+ 

兩個parent_idkid_id將指向person_id 你可以做一個指向型動物在persons_kids表的外鍵型動物模型。

祝你好運。

欲瞭解更多信息,請參閱Cookbook