@@ -154,6 +154,24 @@ public function beforeSave($insert)
154
154
}
155
155
}
156
156
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
+
157
175
/**
158
176
* Author relation
159
177
*
@@ -223,21 +241,14 @@ protected static function buildTree(&$data, $rootID = 0)
223
241
224
242
/**
225
243
* Delete comment.
226
- * All nested comments will also be deleted
227
244
*
228
245
* @return boolean whether comment was deleted or not
229
246
*/
230
247
public function deleteComment ()
231
248
{
232
249
$ this ->status = CommentStatus::DELETED ;
233
250
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 ' ]);
241
252
}
242
253
243
254
/**
@@ -356,4 +367,4 @@ public function getCommentsCount($onlyActiveComments = true)
356
367
357
368
return $ query ->count ();
358
369
}
359
- }
370
+ }
0 commit comments