2014-01-22 38 views
0

我有一個如下的表,其中存儲多個級別的分類。使用單表的多級分類

product_categories (id, name, pid, created, modified) 

PID =的parentID這是相同的表的ID的引用。當它是一個父類別時,它是0 。

我不知道如何在CakePHP中使用hasMany和belongsTo關係來配置此功能。

任何關於如何讓這個工作的指針?

public $hasMany = array(
    'SubCategory' => array(
     'className' => 'ProductCategory', 
     'foreignKey' => 'id', 
     'dependent' => false, 
     'conditions' => '', 
     'fields' => '', 
     'order' => '', 
     'limit' => '', 
     'offset' => '', 
     'exclusive' => '', 
     'finderQuery' => '', 
     'counterQuery' => '' 
    ) 
); 

public $belongsTo = array(
    'ProductCategory' => array(
     'className' => 'ProductCategory', 
     'foreignKey' => 'pid', 
     'conditions' => '', 
     'fields' => '', 
     'order' => '' 
    ) 
); 

回答

1

,如果你想使用一個表,你可以用最簡單的表idparent_id領域(有些喜歡你),並使用find('threaded');

或者

使用TreeBehavior,它是建立在蛋糕的核心。

對於此解決方案,沒有重複使用兩個別名/關係的一個模型。