@@ -68,13 +68,12 @@ public function updateMap($nodeList) {
68
68
$ result = true ;
69
69
70
70
try {
71
- $ flattenTree = $ this ->flattenNestable ($ nodeList );
71
+ $ flattenTree = $ self ->flattenNestable ($ nodeList );
72
72
73
- foreach ($ flattenTree as $ branch ) {
73
+ foreach ($ flattenTree as $ node ) {
74
74
$ self ->node ->flushEventListeners ();
75
- $ model = $ self ->node ->find ($ branch ['id ' ]);
76
- $ model ->fill ($ branch );
77
- $ model ->save ();
75
+ $ model = $ self ->node ->findOrFail (array_get ($ node , $ self ->node ->getKeyName ()));
76
+ $ model ->update ($ node );
78
77
}
79
78
} catch (\Exception $ e ) {
80
79
$ result = false ;
@@ -89,40 +88,38 @@ public function updateMap($nodeList) {
89
88
/**
90
89
* Flattens an array to contain 'id', 'lft', 'rgt', 'depth', 'parent_id' as a valid tree
91
90
* @param $nestableArray
92
- * @param null $parent_id
91
+ * @param mixed $parent_id
93
92
* @param int $depth
94
93
* @param int $bound
95
94
* @return array
96
95
*/
97
- public function flattenNestable ($ nestableArray , $ parent_id = null , $ depth = 0 , &$ bound = 0 )
96
+ public function flattenNestable ($ nodeList , $ parent_id = null , $ depth = 0 , &$ bound = 0 )
98
97
{
99
- $ return = array ();
100
-
101
- foreach ($ nestableArray as $ subArray ) {
102
- $ returnSubSubArray = array ();
98
+ $ nodes = array ();
103
99
100
+ foreach ($ nodeList as $ node ) {
104
101
$ lft = ++$ bound ;
105
102
106
- if (isset ($ subArray ['children ' ])) {
107
- $ returnSubSubArray = $ this ->flattenNestable ($ subArray ['children ' ], $ subArray ['id ' ], ($ depth + 1 ), $ bound );
108
- $ rgt = $ bound + 1 ;
109
- ++$ bound ;
110
- } else {
111
- $ rgt = ++$ bound ;
103
+ $ children = array_get ($ node , $ this ->childrenKeyName , []);
104
+
105
+ if (!empty ($ children )) {
106
+ $ children = $ this ->flattenNestable ($ children , array_get ($ node , $ this ->node ->getKeyName ()), $ depth + 1 , $ bound );
112
107
}
113
108
114
- $ return [] = array (
115
- $ this ->node ->getKeyName () => $ subArray ['id ' ],
109
+ $ rgt = ++$ bound ;
110
+
111
+ $ nodes [] = array (
112
+ $ this ->node ->getKeyName () => array_get ($ node , $ this ->node ->getKeyName ()),
116
113
$ this ->node ->getParentColumnName () => $ parent_id ,
117
114
$ this ->node ->getDepthColumnName () => $ depth ,
118
115
$ this ->node ->getLeftColumnName () => $ lft ,
119
116
$ this ->node ->getRightColumnName () => $ rgt ,
120
117
);
121
118
122
- $ return = array_merge ($ return , $ returnSubSubArray );
119
+ $ nodes = array_merge ($ nodes , $ children );
123
120
}
124
121
125
- return $ return ;
122
+ return $ nodes ;
126
123
}
127
124
128
125
/**
0 commit comments