我使用一个单一的“类别”表中,并且具有parent_id
字段引用表中的任何其他种类的风扇。
这允许您在单个表中包含所有线程类别......您的“类别组”只是任何在其parent_id字段中没有数据的类别行。
实例数据
id name parent_id category_count
1 appliances null 1
2 dogs null 1
3 couch 1 0
4 pitbull 2 0
5 games null 0
//...
基本上, '类别' 是关系到本身 - 这里的模型关联: (参考:在CakePHP book的3rd code example here)
class Category extends AppModel {
public $belongsTo = array(
'Parent' => array(
'className' => 'Category',
'foreignKey' => 'parent_id',
'counterCache' => true
)
);
public $hasMany = array(
'Children' => array(
'className' => 'Category',
'foreignKey' => 'parent_id'
)
);
//...
}
class Item extends AppModel {
public $belongsTo = array('Category');
//...
}
你也可以做Item hasAndBelongsToMany Category
而不是belongsTo
,如果你愿意 - 取决于你的设置。
附加信息:
CounterCache:有关CakePHP的counterCache,go here更多信息。
find->螺纹:使用这种方法,你可以轻松地获得通过嵌套数据CakePHP的find->螺纹(click here for more info about this)
谢谢你的@Sevak我增加了第四台处理类别组。什么是(FK)和(PK)btw? – MotoX 2012-03-13 13:38:32
PK =主键; FK =外键(=引用表的主键)。 – dgw 2012-03-13 13:52:57
是啊@dgw是正确的...... – 2012-03-14 04:06:19