Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving nodes between scopes #58

Open
tremby opened this issue Mar 27, 2014 · 6 comments
Open

Moving nodes between scopes #58

tremby opened this issue Mar 27, 2014 · 6 comments

Comments

@tremby
Copy link

tremby commented Mar 27, 2014

I was having this issue on master, and have just upgraded to develop and have the same behaviour.

I have a drag-and-drop front end to a nested menu system, with multiple menus (scopes) visible on the same page. The user can drag menu items from one menu to another.

We've been seeing some issues with items suddenly disappearing after dragging from menu to menu, and a look at the database shows that the depth is being incremented for unknown reasons.

I tried some testing on my own environment and I noticed pretty early that certain values aren't being updated in one situation. That situation is as follows:

I start with my default menu hierarchy (fresh from db:seed, this is stock and the numbers all look right). Two of the menus roots are empty (footer_left and sub).

+-----+--------------+-----------+------+------+-------+----------------------+
| id  | menu         | parent_id | lft  | rgt  | depth | name                 |
+-----+--------------+-----------+------+------+-------+----------------------+
| 561 | footer_left  |      NULL |    1 |    2 |     0 | NULL                 |
| 562 | footer_right |      NULL |    1 |   16 |     0 | NULL                 |
| 563 | footer_right |       562 |    2 |    3 |     1 | Food & Drink         |
| 564 | footer_right |       562 |    4 |    5 |     1 | Accommodations       |
| 565 | footer_right |       562 |    6 |    7 |     1 | Event Calendar       |
| 566 | footer_right |       562 |    8 |    9 |     1 | Things to Do         |
| 567 | footer_right |       562 |   10 |   11 |     1 | Travel Deals         |
| 568 | footer_right |       562 |   12 |   13 |     1 | Itineraries          |
| 569 | footer_right |       562 |   14 |   15 |     1 | Blog                 |
| 550 | main         |      NULL |    1 |   20 |     0 | NULL                 |
| 551 | main         |       550 |    2 |    9 |     1 | Festivals and Events |
| 552 | main         |       551 |    3 |    4 |     2 | Food & Drink         |
| 553 | main         |       551 |    5 |    6 |     2 | Accommodations       |
| 554 | main         |       551 |    7 |    8 |     2 | Event Calendar       |
| 555 | main         |       550 |   10 |   17 |     1 | Things to Do         |
| 556 | main         |       555 |   11 |   12 |     2 | Food & Drink         |
| 557 | main         |       555 |   13 |   14 |     2 | Accommodations       |
| 558 | main         |       555 |   15 |   16 |     2 | Event Calendar       |
| 559 | main         |       550 |   18 |   19 |     1 | Travel Deals         |
| 560 | sub          |      NULL |    1 |    2 |     0 | NULL                 |
+-----+--------------+-----------+------+------+-------+----------------------+

I drag the "Food & Drink" item (523) from the footer_right menu to the footer_left menu. I verify that the input I am sending to my back end is right -- I'm sending menu, parent_id, lft, rgt, depth for each item ID.

My back end code loads all the MenuItem objects and orders them by menu and then lft. I loop through once and update the 'menu' property of each, in case any need to change scope. Then I loop through in that order again and for each one except root nodes (whether it's been edited or not -- detecting reorderings etc seems like a lot of work) I reload $node and $parentNode to ensure data is fresh and then run $node->makeChildOf($parentNode). This works just fine and reorders things as expected as long as I'm just reordering within a particular menu/scope, but in this example where I'm moving from one scope to another I get an unexpected result.

+-----+--------------+-----------+------+------+-------+----------------------+
| id  | menu         | parent_id | lft  | rgt  | depth | name                 |
+-----+--------------+-----------+------+------+-------+----------------------+
| 561 | footer_left  |      NULL |    1 |    2 |     0 | NULL                 |
| 563 | footer_left  |       562 |    2 |    3 |     1 | Food & Drink         |
| 562 | footer_right |      NULL |    1 |   16 |     0 | NULL                 |
| 564 | footer_right |       562 |    4 |    5 |     1 | Accommodations       |
| 565 | footer_right |       562 |    6 |    7 |     1 | Event Calendar       |
| 566 | footer_right |       562 |    8 |    9 |     1 | Things to Do         |
| 567 | footer_right |       562 |   10 |   11 |     1 | Travel Deals         |
| 568 | footer_right |       562 |   12 |   13 |     1 | Itineraries          |
| 569 | footer_right |       562 |   14 |   15 |     1 | Blog                 |
| 550 | main         |      NULL |    1 |   20 |     0 | NULL                 |
| 551 | main         |       550 |    2 |    9 |     1 | Festivals and Events |
| 552 | main         |       551 |    3 |    4 |     2 | Food & Drink         |
| 553 | main         |       551 |    5 |    6 |     2 | Accommodations       |
| 554 | main         |       551 |    7 |    8 |     2 | Event Calendar       |
| 555 | main         |       550 |   10 |   17 |     1 | Things to Do         |
| 556 | main         |       555 |   11 |   12 |     2 | Food & Drink         |
| 557 | main         |       555 |   13 |   14 |     2 | Accommodations       |
| 558 | main         |       555 |   15 |   16 |     2 | Event Calendar       |
| 559 | main         |       550 |   18 |   19 |     1 | Travel Deals         |
| 560 | sub          |      NULL |    1 |    2 |     0 | NULL                 |
+-----+--------------+-----------+------+------+-------+----------------------+

So, id 563, the one I moved, has changed scope. But note that its parent_id has not changed. Its lft and rgt happen to be right, but they're the same as they were before (which was also right). The depth is right. But also note that the footer_left root's lft and rgt have not been updated -- in particular, the rgt should now be 4. footer_right's subtree hasn't updated its lft and rgt values either, but I'm not sure if that's an issue or not.

So something above is broken.

It gets interesting when I move a different menu item. Re-seeding the database I have this:

+-----+--------------+-----------+------+------+-------+----------------------+
| id  | menu         | parent_id | lft  | rgt  | depth | name                 |
+-----+--------------+-----------+------+------+-------+----------------------+
| 581 | footer_left  |      NULL |    1 |    2 |     0 | NULL                 |
| 582 | footer_right |      NULL |    1 |   16 |     0 | NULL                 |
| 583 | footer_right |       582 |    2 |    3 |     1 | Food & Drink         |
| 584 | footer_right |       582 |    4 |    5 |     1 | Accommodations       |
| 585 | footer_right |       582 |    6 |    7 |     1 | Event Calendar       |
| 586 | footer_right |       582 |    8 |    9 |     1 | Things to Do         |
| 587 | footer_right |       582 |   10 |   11 |     1 | Travel Deals         |
| 588 | footer_right |       582 |   12 |   13 |     1 | Itineraries          |
| 589 | footer_right |       582 |   14 |   15 |     1 | Blog                 |
| 570 | main         |      NULL |    1 |   20 |     0 | NULL                 |
| 571 | main         |       570 |    2 |    9 |     1 | Festivals and Events |
| 572 | main         |       571 |    3 |    4 |     2 | Food & Drink         |
| 573 | main         |       571 |    5 |    6 |     2 | Accommodations       |
| 574 | main         |       571 |    7 |    8 |     2 | Event Calendar       |
| 575 | main         |       570 |   10 |   17 |     1 | Things to Do         |
| 576 | main         |       575 |   11 |   12 |     2 | Food & Drink         |
| 577 | main         |       575 |   13 |   14 |     2 | Accommodations       |
| 578 | main         |       575 |   15 |   16 |     2 | Event Calendar       |
| 579 | main         |       570 |   18 |   19 |     1 | Travel Deals         |
| 580 | sub          |      NULL |    1 |    2 |     0 | NULL                 |
+-----+--------------+-----------+------+------+-------+----------------------+

If I now move 'Accommodations' (584) from footer_right to footer_left, I end up with this:

+-----+--------------+-----------+------+------+-------+----------------------+
| id  | menu         | parent_id | lft  | rgt  | depth | name                 |
+-----+--------------+-----------+------+------+-------+----------------------+
| 581 | footer_left  |      NULL |    1 |    4 |     0 | NULL                 |
| 584 | footer_left  |       581 |    2 |    3 |     1 | Accommodations       |
| 582 | footer_right |      NULL |    1 |   16 |     0 | NULL                 |
| 583 | footer_right |       582 |    4 |    5 |     1 | Food & Drink         |
| 585 | footer_right |       582 |    6 |    7 |     1 | Event Calendar       |
| 586 | footer_right |       582 |    8 |    9 |     1 | Things to Do         |
| 587 | footer_right |       582 |   10 |   11 |     1 | Travel Deals         |
| 588 | footer_right |       582 |   12 |   13 |     1 | Itineraries          |
| 589 | footer_right |       582 |   14 |   15 |     1 | Blog                 |
| 570 | main         |      NULL |    1 |   20 |     0 | NULL                 |
| 571 | main         |       570 |    2 |    9 |     1 | Festivals and Events |
| 572 | main         |       571 |    3 |    4 |     2 | Food & Drink         |
| 573 | main         |       571 |    5 |    6 |     2 | Accommodations       |
| 574 | main         |       571 |    7 |    8 |     2 | Event Calendar       |
| 575 | main         |       570 |   10 |   17 |     1 | Things to Do         |
| 576 | main         |       575 |   11 |   12 |     2 | Food & Drink         |
| 577 | main         |       575 |   13 |   14 |     2 | Accommodations       |
| 578 | main         |       575 |   15 |   16 |     2 | Event Calendar       |
| 579 | main         |       570 |   18 |   19 |     1 | Travel Deals         |
| 580 | sub          |      NULL |    1 |    2 |     0 | NULL                 |
+-----+--------------+-----------+------+------+-------+----------------------+

Again, I'm not sure if it's an issue that footer_right root and children's lft and rgt values haven't changed, but this time the moved node 584 has the correct parent_id, lft and rgt, and the footer_left root 581 has the correct lft and rgt too.

The issue above occurs whether I run MenuItem::rebuild() after all this or not. I also tried running MenuItem::rebuild() between passes (after I change the scopes where necessary), and I have the same issue.

Is there something I'm doing wrong? Or is this a bug?

@xgenvn
Copy link

xgenvn commented Apr 2, 2014

I'm not sure to be so sophisticated in your case. In my simple case:

  • Create a category: News Category.
    Check the created item by using $node->isRoot(), I got return false (this is a bug?)
  • Add another child category under News, name it Technology.
    Check the News node again $node->isRoot(), also return false.
  • Use makeRoot() to make Technology become root node, I got exception

    Baum \ MoveNotPossibleException
    Could not resolve target node. This node cannot move any further to the right.

So it's possibly a bug around here, just in this simple case.
Maybe it's worse in your case. I can work around by not move it to be root node (which is rarely done), just delete and create new one if necessary.

@tremby
Copy link
Author

tremby commented Apr 2, 2014

Sorry, but it really doesn't sound like your issue is connected. Perhaps
open another ticket.

@xgenvn
Copy link

xgenvn commented Apr 2, 2014

Sorry if my comment messed you up. My point is that I don't think it's stable to use multi-root-nodes in your case (and mine too perhaps). The best possibility is to work under the sole root node. Can you test the case that all of your the current roots to be under one root node, ie: main, footer_right, sub are under ROOTMENU node. Because in my simple case, it's also using moveLeft and moveRight methods, the function should also affect your case if there's a bug right there.

@tremby
Copy link
Author

tremby commented Apr 2, 2014

I was under the impression that multiple roots need to be in different
scopes, which mine are. It doesn't sound like you're using scopes.

@etrepat
Copy link
Owner

etrepat commented May 13, 2014

I understand the "move between scopes" functionality has some issues.

I'm trying to make this work... I'll prepare an specific test case and work from that.

@Machecek
Copy link

@etrepat any progress on "move between scopes" functionality?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants