0

I have used the nestedSortable jQuery plugin (https://github.com/ilikenwf/nestedSortable) to order my categories with drag and drop. Every time a category is been ordered an Ajax request is going to the controller with a post of an array with the complete data of the categories. Everything is working as expected so for;

Array received in controller by Ajax post:

Array
(
    [0] => Array
        (
            [item_id] => 
            [parent_id] => 
            [depth] => 0
            [left] => 1
            [right] => 8
        )

    [1] => Array
        (
            [id] => 1
            [parent_id] => 
            [depth] => 0
            [left] => 2
            [right] => 3
        )

    [2] => Array
        (
            [id] => 4
            [parent_id] => 
            [depth] => 0
            [left] => 4
            [right] => 5
        )

    [3] => Array
        (
            [id] => 2
            [parent_id] => 
            [depth] => 0
            [left] => 6
            [right] => 7
        )
)

In my controller I have the following function to process:

public function reorder() {
    $list = isset($_POST['_list']) ? json_decode($_POST['_list'], true) : null;
    foreach ($list as $item) {
        if (!empty($item['id'])) {

            $category = $this->Categories->get($item['id']);

            if (!empty($category)) {
                empty($item['parent_id']) ? $category->parent_id = null : $category->parent_id = $item['parent_id'] ;
                $category->lft = $item['left'];
                $category->rght = $item['right'];

                $this->Categories->save($category);
            }
        }
    }
}

When I debug the categories before saving the lft and rght are set correctly, however after saving the lft and rght contain completly different values.

I assume that since TreeBehavior is implemented the save function is doing some checks and resetting lft and rght to other values? Anyway to skip this?

David
  • 11
  • 3
  • 1
    That's the complete tree that you're receiving I assume? I think you'd have to unload the tree behavior when saving, as you're basically doing its work in the frontend already. ps, with CakePHP don't use superglobals like `$_POST` directly, use the abstracted interface on the request object instead! – ndm Feb 10 '20 at 17:12
  • If you want to stop TreeBehavior from acting on your data: https://book.cakephp.org/4/en/orm/behaviors.html#removing-loaded-behaviors But the point of the behavior is to manage these values to create the tree structure you request. If your manually set lft/rt values don't match what Tree sets, there seems to be a disconnect between Tree's design principles and your design assumption. Are you sure you understand the structure the behavior is making? If you disable it once to maintain your settings, you may never be able to use it again for a save. – Don Drake Feb 11 '20 at 06:03
  • The issue is that I want to be able to move nodes between parents and also more then 1 up or down movement at a time. – David Feb 11 '20 at 10:22
  • Yes, I can see the need for that. But I still need to ask, are you sure you understand the structure you are making and the pattern of the behavior? If you make your edits, unload the behavior, save your data, then reload the behavior and get the `treeList()`, is your result what you expected? If so you can continue to turn it off/on for your processes. Or write an extension to the behavior that supports the new abilities you need. – Don Drake Feb 11 '20 at 16:55
  • One final confirmation that your edits conform to Tree internal expectations would be running `recover` on your edited data. If it STILL retains your expected tree structure, then your changes are valid. – Don Drake Feb 11 '20 at 19:48
  • Yes, the information saved to the DB is correct and working. Also it appears for some rease that when I loop through the categories from top to bottom and set lft and rght to NULL with tree behavior active and save the categories that cakephp is also making all values correct... – David Feb 11 '20 at 21:38
  • Do you feel like you have a working solution then? – Don Drake Feb 11 '20 at 23:57

0 Answers0