I want to get a tree of Categories to show it like this:
cat1
subcat1
subcat2
subcat3
subsubcat1
cat2
subcat1
...
I have this table:
CREATE TABLE IF NOT EXISTS `kategorie` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(256) collate utf8_polish_ci NOT NULL,
`father` int(11) NOT NULL default '-7',
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
)
So each record contains only self and parent id (I cannot add left and right ids).
For now I made this model:
class Category extends AppModel
{
public $name = 'Category';
public $useDbConfig = 'external';
public $useTable = 'kategorie';
var $belongsTo = array(
'ParentCategory' => array(
'className' => 'Category',
'foreignKey' => 'father'
));
var $hasMany = array(
'ChildCategory' => array(
'className' => 'Category',
'foreignKey' => 'father'
));
}
and controller:
class CategoriesController extends AppController {
public function beforeFilter()
{
parent::beforeFilter();
//$this->Auth->allow('add','logout');
}
public function index()
{
$res = $this->Category->find('all');
//$this->set('categories', $this->Category->find('all') );
debug($res); die;
}
}
The debug output is something like this:
array(
(int) 0 => array(
'Category' => array(
'id' => '1',
'name' => 'catname',
'father' => '2'
),
'ParentCategory' => array(
'id' => '2',
'name' => 'catname',
'father' => '3'
),
'ChildCategory' => array(
(int) 0 => array(
'id' => '161',
'name' => 'catname',
'father' => '1'
)
)
),
(int) 1 => array(
'Category' => array(
'id' => '2',
'name' => 'catname',
'father' => '3'
),
'ParentCategory' => array(
'id' => '3',
'name' => 'catname',
'father' => '4'
),
'ChildCategory' => array(
(int) 0 => array(
'id' => '1',
'name' => 'catname',
'father' => '2'
),
(int) 1 => array(
'id' => '5',
'name' => 'catname',
'father' => '2'
),
(int) 2 => array(
'id' => '489',
'name' => 'catname',
'father' => '2'
)
)
),
...
So basicly I get array of Category objects (the number of these objects is the number of categories (all, even subcategories). Each category has a parent category and array of child categories...
This is not what I want because I would have to "manually" parse the array to build a category tree and it would be slow as I would have to find categories with specified id in the array (category id is not it's index in array)...
Maybe some1 knows any special trick to get a nice, useful category tree?