creocoder / yii2-nested-sets

The nested sets behavior for the Yii framework.
Other
446 stars 129 forks source link

Tree breaks when moving #90

Open dkhlystov opened 8 years ago

dkhlystov commented 8 years ago

Data before:

CREATE TABLE IF NOT EXISTS `Menu` (
  `id` int(10) NOT NULL,
  `lft` int(11) NOT NULL,
  `rgt` int(11) NOT NULL,
  `depth` int(11) NOT NULL,
  `active` tinyint(1) DEFAULT '1',
  `name` varchar(50) NOT NULL,
  `url` varchar(100) DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
INSERT INTO `Menu` (`id`, `lft`, `rgt`, `depth`, `active`, `name`, `url`) VALUES
(1, 1, 8, 0, 1, 'Root', NULL),
(2, 3, 4, 2, 1, 'Node 1-1', NULL),
(3, 2, 5, 1, 1, 'Node 1', NULL),
(5, 6, 7, 1, 1, 'Node 2', NULL);

Moving code:

$model = Menu::findOne(2);
$target = Menu::findOne(5);
$model->insertAfter($target);
$model->insertBefore($target);

Data after:

id  lft rgt depth   active  name    url
1   1   8   0   1   Root    NULL
3   2   4   1   1   Node 1  NULL
5   4   5   1   1   Node 2  NULL
2   6   7   1   1   Node 1-1    NULL

On 'Node 1' (id:3) there are wrong 'rgt' value

ebuzzz commented 8 years ago

In my experience, moving is a hard thing to get right with these kind of nested sets. I haven't used this specific implementation, but normally you're better of rebuilding the tree from the first level both nodes (old and new) are children of.

dkhlystov commented 8 years ago

It is unlikely that the code I wrote is used. But this is extension bug.