I have a table as follows which stores categories in multiple Level.

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

pid = parentID which is a reference to the same table's ID. it is 0 when its a parent Category.

I am not sure how i can configure this using hasMany and belongsTo relation in CakePHP.

Any pointers on how i can get this to work?

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' => ''
    )
);
有帮助吗?

解决方案

If You want using in one table, You can use simplest table with id and parent_id fields (some like You have) and using find('threaded');

Or

Use TreeBehavior, it is built in core of Cake.

There no reasoun to use two alias/relations for one model for this solution.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top