@@ -119,7 +119,7 @@ public function fetchRootByName($name)
119
119
* @return void
120
120
* @author Brent Shaffer
121
121
*/
122
- public function clearTree ($ root )
122
+ public function clearTree (ioDoctrineMenuItem $ root )
123
123
{
124
124
$ nodes = $ this ->createQuery ()
125
125
->where ('root_id = ? ' , $ root ['id ' ])
@@ -142,34 +142,46 @@ public function clearTree($root)
142
142
* @return void
143
143
* @author Brent Shaffer
144
144
*/
145
- public function restoreTreeFromNestedArray ($ arr , $ name )
145
+ public function restoreTreeFromNestedArray ($ arr , ioDoctrineMenuItem $ root )
146
146
{
147
- $ root = $ this ->fetchRootByName ($ name );
147
+ if (!$ root ->getNode ()->isRoot ())
148
+ {
149
+ throw new sfException ('ioDoctrineMenuItemTable::restoreTreeFromNestedArray() must be called using a root node. ' );
150
+ }
148
151
152
+ // remove all of the nodes from the tree
149
153
$ this ->clearTree ($ root );
150
154
151
- Doctrine::getTable ('ioDoctrineMenuItem ' )->getTree ()->createRoot ($ root );
152
-
153
- $ this ->restoreBranchFromNestedArray (array ('id ' => $ root ['id ' ], 'children ' => $ arr ));
155
+ // reestablish the root
156
+ $ this ->getTree ()->createRoot ($ root );
157
+
158
+ // put the nodes back on
159
+ $ this ->restoreBranchFromNestedArray (array ('menu ' => $ root , 'children ' => $ arr ));
154
160
}
155
-
161
+
156
162
/**
157
- * recursive function to create a nested set from an array
163
+ * recursive function to create a nested set sourced from an array
158
164
*
159
- * @param string $arr
160
- * @return void
165
+ * @param array $arr The source array with keys menu and children
166
+ * @return ioDoctrineMenuItem
161
167
* @author Brent Shaffer
162
168
*/
163
169
public function restoreBranchFromNestedArray ($ arr )
164
170
{
165
- $ parent = Doctrine:: getTable ( ' ioDoctrineMenuItem ' )-> findOneById ( $ arr ['id ' ]) ;
171
+ $ parent = $ arr ['menu ' ] ;
166
172
167
- if (isset ($ arr ['children ' ]))
173
+ if (isset ($ arr ['children ' ]))
168
174
{
169
- foreach ($ arr ['children ' ] as $ childArr )
175
+ foreach ($ arr ['children ' ] as $ childArr )
170
176
{
171
- $ child = Doctrine:: getTable ( ' ioDoctrineMenuItem ' )-> findOneById ($ childArr ['id ' ]);
177
+ $ child = $ this -> find ($ childArr ['id ' ]);
172
178
$ child ->getNode ()->insertAsLastChildOf ($ parent );
179
+
180
+ // put the child object into the array
181
+ $ childArr ['menu ' ] = $ child ;
182
+ unset($ childArr ['id ' ]);
183
+
184
+ // recurse down and ultimately refresh the parent
173
185
$ this ->restoreBranchFromNestedArray ($ childArr );
174
186
$ parent ->refresh ();
175
187
}
0 commit comments