/**
  * On User delete, also delete all comments
  *
  * @param type $event
  */
 public static function onUserDelete($event)
 {
     foreach (MessageEntry::findAll(array('user_id' => $event->sender->id)) as $messageEntry) {
         $messageEntry->delete();
     }
     foreach (UserMessage::findAll(array('user_id' => $event->sender->id)) as $userMessage) {
         $userMessage->message->leave($event->sender->id);
     }
     return true;
 }
 /**
  * Deletes a message, including all dependencies.
  */
 public function delete()
 {
     foreach (MessageEntry::findAll(array('message_id' => $this->id)) as $messageEntry) {
         $messageEntry->delete();
     }
     foreach (UserMessage::findAll(array('message_id' => $this->id)) as $userMessage) {
         $userMessage->delete();
     }
     parent::delete();
 }
 public function up()
 {
     $this->renameClass('MessageEntry', MessageEntry::className());
 }
 /**
  * Delete Entry Id
  *
  * Users can delete the own message entries.
  */
 public function actionDeleteEntry()
 {
     $this->forcePostRequest();
     $messageEntryId = (int) Yii::$app->request->get('messageEntryId');
     $entry = MessageEntry::findOne(['id' => $messageEntryId]);
     // Check if message entry exists and it´s by this user
     if ($entry == null || $entry->user_id != Yii::$app->user->id) {
         throw new HttpException(404, 'Could not find message entry!');
     }
     $entry->message->deleteEntry($entry);
     if (Yii::$app->request->isAjax) {
         return $this->htmlRedirect(['index', 'id' => $entry->message_id]);
     } else {
         return $this->redirect(['index', 'id' => $entry->message_id]);
     }
 }