@@ -154,6 +154,24 @@ public function beforeSave($insert)
154154 }
155155 }
156156
157+ /**
158+ * This method is called at the end of inserting or updating a record.
159+ *
160+ * @param bool $insert
161+ * @param array $changedAttributes
162+ */
163+ public function afterSave ($ insert , $ changedAttributes )
164+ {
165+ if (!$ insert ) {
166+ // Mark all the nested comments as `deleted` after the comment was deleted
167+ if (array_key_exists ('status ' , $ changedAttributes ) && $ this ->status == CommentStatus::DELETED ) {
168+ self ::updateAll (['status ' => CommentStatus::DELETED ], ['parentId ' => $ this ->id ]);
169+ }
170+ }
171+
172+ parent ::afterSave ($ insert , $ changedAttributes );
173+ }
174+
157175 /**
158176 * Author relation
159177 *
@@ -223,21 +241,14 @@ protected static function buildTree(&$data, $rootID = 0)
223241
224242 /**
225243 * Delete comment.
226- * All nested comments will also be deleted
227244 *
228245 * @return boolean whether comment was deleted or not
229246 */
230247 public function deleteComment ()
231248 {
232249 $ this ->status = CommentStatus::DELETED ;
233250
234- if ($ this ->save (false , ['status ' , 'updatedBy ' , 'updatedAt ' ])) {
235- // Mark all the nested comments as deleted
236- self ::updateAll (['status ' => CommentStatus::DELETED ], ['parentId ' => $ this ->id ]);
237- return true ;
238- }
239-
240- return false ;
251+ return $ this ->save (false , ['status ' , 'updatedBy ' , 'updatedAt ' ]);
241252 }
242253
243254 /**
@@ -356,4 +367,4 @@ public function getCommentsCount($onlyActiveComments = true)
356367
357368 return $ query ->count ();
358369 }
359- }
370+ }
0 commit comments