Create A Menu Tree From Db Parent

Hello,

I need to create an array that contain all elements organized by a tree of parents and childs

Heres my table:


CREATE TABLE IF NOT EXISTS `menu` (

  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,

  `active` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT 'Menu visibility',

  `menutypeid` int(10) unsigned NOT NULL,

  [b]`parentid` int(10) unsigned DEFAULT NULL,[/b]

  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT 'Name or title',

  `alias` varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT 'Unique identifier',

  `link` varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT 'Display or navigation link',

  `tbicon` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'Twitter Boostrap class icon',

  `image` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'Image to display with menu name',

  `order` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT 'Show relative order',

  PRIMARY KEY (`id`),

  KEY `order` (`order`),

  KEY `active` (`active`),

  KEY `parentid` (`parentid`),

  KEY `menutypeid` (`menutypeid`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

for example:

Now im stuck on it, Note: it can have more levels, unlimited in that case

Also im not using AR design because performance and speed. im getting all rows from database at once.

Any ideia to do that easy?

Thanks

If you are going to use it mainly for reading - you could look at Nested Set data structure - http://en.wikipedia.org/wiki/Nested_set_model .

There is also YII extension for this - http://www.yiiframework.com/extension/nestedsetbehavior/ - though I haven’t used it.

AS i say i dont want to use AR

i always see that models use lft and rgt attributes but i never understand for what, since we can have more than a root and more than 2 childs.

Thanks