0
votes

I am just getting started with the CakePHP 3 Tree behaviour (https://book.cakephp.org/3.0/en/orm/behaviors/tree.html). I have a situation where I have an existing tree and I would like to reorder them based on an array. I tried it like this:

        $data = [
            [
                'id' => 1,
                'parent_id' => null,
                'children' => [
                    [
                        'id' => 3,
                        'children' => [
                            'id' => 4,
                            'children' => [
                                'id' => 5
                            ]
                        ]
                    ]
                ]
            ],
            [
                'id' => 2,
                'parent_id' => false
            ],
        ];

        $minutesTable->patchEntities($minutes, $data);

        $minutesTable->saveMany($minutes);

...Hoping that the children field would work, but unfortunately no. The attempt above doesn't return an error, but the children field is simply ignored.

Is there a built-in way to achieve this? What would an elegant alternative be?

Just to add: changing an individual parent_id works in my application. What I am looking for is completely restructuring the tree using a reference array.

1
What problem are you trying to solve by using a reference array? There is no such built-in functionality, you'd have to figure something on your own. Given how updating nested sets works, doing multiple moves/saves/deletes is probably inevitable. - ndm
I want to save the state of a JQuery nested sortable list. I figured to easiest way was to convert the nested list into an array and go from there. But if that's not possible I could instead use individual operations. - Roberto

1 Answers

-1
votes

I think you should find out association in cakephp. Link : Cakephp 3 Association

For example You have one parent and many children, and children have many children. So, Parent will become the mean data. And those table must have relationship between each other. I will override your array as the Example:

$data = array:2 [
   0 => array:2 [
      "parent_id" => "1"
      "name" => "Mr. A"
      "childrens" => array:2 [
          0 => array:2 [
            "id" => "32"
            "parent_id" => "1"
            "name" => "Mr. A SON"
            "grand_childrens" => array:1 [
                0 => array:0 [
                "id" => "4"
                "grand_children_id" => "32"
                "name" => "Mr. A GRAND SON"
                ]
            ]
          ],
          1 => array:0 [
            "id" => "2"
            "parent_id" => "1"
            "name" => "Mr. A Daughter"
          ]
      ]

    ],
    1 => array:2 [
      "parent_id" => "2"
      "name" => "Mr. B"
      "childrens" => array:2 [
          0 => array:2 [
            "id" => "5"
            "parent_id" => "2"
            "name" => "Mr. B SON"
            "grand_childrens" => array:1 [
                0 => array:0 [
                "id" => "7"
                "grand_children_id" => "5"
                "name" => "Mr. B GRAND SON"
                ]
            ]
          ],
          1 => array:0 [
            "id" => "4"
            "parent_id" => "2"
            "name" => "Mr. B Daughter"
          ]
      ]

    ]

]