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?