PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » Database » DB_NestedSet » Bug #2034

getAllNodes with secondarySort='name') and setsortMode='NESE_SORT_PREORDER' do

Details

Submitted2004-07-31 12:40 UTC
Fromstiwi at lukasonline dot com
Assigneddatenpunk
StatusClosed
PackageDB_NestedSet
PHP Version4.3.8
OSLinux
Roadmaps(Not assigned)

Comments

[2004-07-31 12:40 UTC] stiwi at lukasonline dot com

Description:
------------
If I read my whole Tree with getAllNodes(true) and setSortMode(NESE_SORT_PREORDER) I'll get the following:

-Skireisen
--Schweiz
---Grindelwald
--Österreich
---Arlberg
---Saalbach
---Obertauern
--Italien
---Meransen

Wich is the expected result.

Now if I'm adding a "'secondarySort'=>'name'" to the attribute-array, to get the sibblings in alphabetic order I'll get this:

-Skireisen
--Österreich
--Schweiz
---Arlberg
---Grindelwald
---Meransen
---Obertauern
---Saalbach
--Italien

Wich is not the result I expect! I'm using 1.35beta

[2004-07-31 19:19 UTC] dk at webcluster dot at

Please add a sql dump of this tree and a short code snipplet to help me debug this issue.

Thanks for your help

Daniel Khan

[2004-07-31 21:01 UTC] stiwi at lukasonline dot com

Ok, here's the dump and the testcode

CREATE TABLE `categories` (
`id` int(10) unsigned NOT NULL default '0',
`root_id` int(10) unsigned NOT NULL default '0',
`parent_id` int(10) unsigned NOT NULL default '0',
`order_num` tinyint(4) unsigned NOT NULL default '0',
`level` int(10) unsigned NOT NULL default '0',
`left_id` int(10) unsigned NOT NULL default '0',
`right_id` int(10) unsigned NOT NULL default '0',
`name` varchar(60) NOT NULL default '',
PRIMARY KEY (`id`),
KEY `right` (`right_id`),
KEY `left` (`left_id`),
KEY `order` (`order_num`),
KEY `level` (`level`),
KEY `right_left` (`id`,`root_id`,`left_id`,`right_id`),
KEY `parent_root` (`root_id`)
) TYPE=MyISAM;

INSERT INTO `categories` VALUES (1, 1, 0, 1, 1, 1, 18, 'Skireisen');
INSERT INTO `categories` VALUES (2, 1, 1, 1, 2, 2, 5, 'Schweiz');
INSERT INTO `categories` VALUES (3, 1, 1, 3, 2, 14, 17, 'Italien');
INSERT INTO `categories` VALUES (4, 1, 1, 2, 2, 6, 13, 'Österreich');
INSERT INTO `categories` VALUES (5, 1, 3, 1, 3, 15, 16, 'Meransen');
INSERT INTO `categories` VALUES (6, 1, 4, 1, 3, 7, 8, 'Arlberg');
INSERT INTO `categories` VALUES (7, 1, 2, 1, 3, 3, 4, 'Grindelwald');
INSERT INTO `categories` VALUES (8, 1, 4, 2, 3, 9, 10, 'Saalbach');
INSERT INTO `categories` VALUES (9, 1, 4, 3, 3, 11, 12, 'Obertauern');

<?php

require_once('DB/NestedSet.php');
require_once('DB/NestedSet/DB.php');

$dsn = "mysql://root:pass@localhost/dbname";

$params = array(
'id' => 'id',
'root_id' => 'rootid',
'parent_id' => 'parent',
'left_id' => 'l',
'right_id' => 'r',
'order_num' => 'norder',
'level' => 'level',
'name' => 'name'
);

$tree = new DB_NestedSet_DB($dsn, $params);

$tree->setAttr(array('node_table' => 'categories', 'lock_table' => 'table_locks', 'secondarySort' => 'name'));

$tree->setSortMode(NESE_SORT_PREORDER);

$nodes = $tree->getAllNodes(true);

echo '<pre>';
foreach($nodes as $key => $value){
unset($line);
for($i = 0; $i< $value['level']; $i++){
$line .= '-';
}
echo $l.$value['name'].'<br>';
}
echo '</pre>';
?>

[2004-07-31 21:08 UTC] stiwi at lukasonline dot com

Oops

echo $l.$value['name'].'<br>';

should be

echo $line.$value['name'].'<br>';

[2004-08-03 22:41 UTC] dk at webcluster dot at

This bug should now be fixed in CVS.
Please have a look if it works for you.

Please consider using output drivers if you want to output the tree for a navigation anything like that. Because although it works now this way of sorting a Nested Set is pretty ugly (recursion).
If you want to output the tree with JavaScript or something like that you might again have to traverse the tree to get it into the desired format. If this is the case the sorting should be done while doing this conversion do avoid traversing the tree 2 times.
However - please try the CVS version :)

Thanks for making PEAR better!

--
Daniel Khan

[2004-08-04 01:11 UTC] stiwi at lukasonline dot com

Hi Daniel,

the CVS version works perfect for me now!

Thanks for your effort

Stiwi

PS: I'm using this kind of parsing the tree only in my admin-frontend for the tree-content. For the enduser tree-navigation I'll use the treeMenu-driver either.

[2004-08-04 12:58 UTC] dk at webcluster dot at

This bug has been fixed in CVS.

In case this was a documentation problem, the fix will show up at the
end of next Sunday (CET) on pear.php.net.

In case this was a pear.php.net website problem, the change will show
up on the website in short time.

Thank you for the report, and for helping us make PEAR better.

[2004-08-08 21:59 UTC] stiwi at lukasonline dot com

Hi David,

there's a little bug in the current CVS-version of NestedSet.php. In line 797 you need to check if $nodeSet is an array with a count > 0 before you call _secSort(), otherwise a call of getSubBranch() will throw an error if the used node has no subbranch.

Stiwi

[2004-08-10 21:50 UTC] dk at webcluster dot at

Thank you for your bug report. This issue has been fixed
in the latest released version of the package, which you can download at
http://pear.php.net/get/DB_NestedSet

Thanks a lot for pointing that out!