@@ -177,6 +177,9 @@ public function processDelete(ModelInterface $model): void
177177 ]);
178178 }
179179
180+ /**
181+ * @param ModelInterface $model
182+ */
180183 public function processUpdate (ModelInterface $ model ): void
181184 {
182185 $ currentModel = $ model ::findFirst ([
@@ -203,16 +206,24 @@ public function processUpdate(ModelInterface $model): void
203206 $ query = 'UPDATE ` ' . $ model ->getSource () . '` SET ' .
204207 '` ' . self ::$ depthDbColumn . '` = ` ' . self ::$ depthDbColumn . '` - 1, ' .
205208 '` ' . self ::$ rightDbColumn . '` = ` ' . self ::$ rightDbColumn . '` - 1, ' .
206- '` ' . self ::$ leftDbColumn . '` = ` ' . self ::$ leftDbColumn . '` - 1, ' .
207- '` ' . self ::$ parentDbColumn . '` = :parentID ' .
209+ '` ' . self ::$ leftDbColumn . '` = ` ' . self ::$ leftDbColumn . '` - 1 ' .
208210 'WHERE ` ' . self ::$ leftDbColumn . '` > :left AND ` ' . self ::$ rightDbColumn . '` < :right ' .
209211 'AND ` ' . self ::$ primaryDbColumn . '` != :id ' ;
210212 $ model ->getWriteConnection ()->query ($ query , [
211213 'right ' => $ currentModel ->readAttribute (self ::$ rightKey ),
212214 'left ' => $ currentModel ->readAttribute (self ::$ leftKey ),
213- 'parentID ' => $ currentModel ->readAttribute (self ::$ parentKey ),
214215 'id ' => $ model ->readAttribute (self ::$ primaryKey ),
215216 ]);
217+
218+ // update parent for immediate sub-nodes of current category
219+ $ query = 'UPDATE ` ' . $ model ->getSource () . '` SET ' .
220+ ' ` ' . self ::$ parentDbColumn . '` = :parent ' .
221+ 'WHERE ` ' . self ::$ parentDbColumn . '` = :id ' ;
222+ $ model ->getWriteConnection ()->query ($ query , [
223+ 'id ' => $ currentModel ->readAttribute (self ::$ primaryKey ),
224+ 'parent ' => $ currentModel ->readAttribute (self ::$ parentKey )
225+ ]);
226+
216227 // update nodes on the right of the tree
217228 $ query = 'UPDATE ` ' . $ model ->getSource () . '` SET ' .
218229 '` ' . self ::$ rightDbColumn . '` = ` ' . self ::$ rightDbColumn . '` - 2 ' .
@@ -243,12 +254,15 @@ public function processUpdate(ModelInterface $model): void
243254 }
244255
245256 $ right = $ parentModel ->readAttribute (self ::$ rightKey );
257+ $ depth = $ parentModel ->readAttribute (self ::$ depthKey );
246258 if ($ parentModel ->readAttribute (self ::$ rightKey ) > $ currentModel ->readAttribute (self ::$ rightKey )
247259 && $ parentModel ->readAttribute (self ::$ rightKey ) > $ currentModel ->readAttribute (self ::$ leftKey )) {
248260 $ right = $ parentModel ->readAttribute (self ::$ rightKey ) - 2 ;
261+ $ depth = $ parentModel ->readAttribute (self ::$ depthKey ) + 1 ;
249262 } elseif ($ parentModel ->readAttribute (self ::$ rightKey ) < $ currentModel ->readAttribute (self ::$ rightKey )
250263 && $ parentModel ->readAttribute (self ::$ rightKey ) > $ currentModel ->readAttribute (self ::$ leftKey )) {
251264 $ right = $ parentModel ->readAttribute (self ::$ rightKey ) - 1 ;
265+ $ depth = $ parentModel ->readAttribute (self ::$ depthKey );
252266 }
253267
254268 $ query = 'UPDATE ` ' . $ model ->getSource () . '` SET ' .
@@ -268,8 +282,8 @@ public function processUpdate(ModelInterface $model): void
268282 $ model ->assign ([
269283 self ::$ leftKey => $ right ,
270284 self ::$ rightKey => $ right + 1 ,
271- self ::$ depthKey => $ parentModel -> readAttribute ( self :: $ depthKey ) + 1 ,
272- self ::$ parentKey => 0 ,
285+ self ::$ depthKey => $ depth ,
286+ self ::$ parentKey => $ parentModel -> readAttribute ( self :: $ primaryKey ) ,
273287 ]);
274288 }
275289}
0 commit comments