0

I am new to Yii2 and its controllers and models. I have installed a app and running but the notifications are not saving in DB. Like is users are commenting or following a article no notification showing because data not storing to DB.

Code Sample:

protected function createNotification($type, $data) {
    switch($type) {
        case 'a2a':
            if($data->asked_to != $data->asked_by) {
                $notification = new UserNotifications;
                $notification->user_id = $data->asked_to;
                $notification->type = UserNotifications::TYPE_A2A;
                $notification->details = Yii::t(
                    'app', 
                    $notification->msgs[$type], [
                        'username' => Html::a($data->askedBy->name, ['users/view', 'id' => $data->asked_by, 'slug' => $data->askedBy->slug]),
                        'question' => Html::a($data->question->title, ['questions/view', 'id' => $data->question_id, 'slug' => $data->question->slug])
                    ]
                );
                $notification->date = date('Y-m-d H:i:s');
                // TBD - check for failures
                $notification->save();
            }
        break;

        case 'upvote':
            if($data->answer->user_id != $data->user_id) {
                $notification = new UserNotifications;
                $notification->user_id = $data->answer->user_id;
                $notification->type = UserNotifications::TYPE_UPVOTE;
                $notification->details = Yii::t(
                    'app', 
                    $notification->msgs[$type], [
                        'username' => Html::a($data->user->name, ['users/view', 'id' => $data->user_id, 'slug' => $data->user->slug]),
                        'question' => Html::a($data->answer->question->title, ['questions/view', 'id' => $data->answer->question_id, 'slug' => $data->answer->question->slug])
                    ]
                );
                $notification->date = date('Y-m-d H:i:s');
                // TBD - check for failures
                $notification->save();
            }
        break;

        case 'follow':
            if($data->follower_id != $data->user_id) {
                $notification = new UserNotifications;
                $notification->user_id = $data->user_id;
                $notification->type = UserNotifications::TYPE_FOLLOW;
                $notification->details = Yii::t(
                    'app', 
                    $notification->msgs[$type], [
                        'username' => Html::a($data->follower->name, ['users/view', 'id' => $data->follower_id, 'slug' => $data->follower->slug])
                    ]
                );
                $notification->date = date('Y-m-d H:i:s');
                // TBD - check for failures
                $notification->save();
            }
        break;

        case 'acomment':
            if($data->answer->user_id != $data->user_id) {
                $notification = new UserNotifications;
                $notification->user_id = $data->answer->user_id;
                $notification->type = UserNotifications::TYPE_ANSWER_COMMENT;
                $notification->details = Yii::t(
                    'app', 
                    $notification->msgs[$type], [
                        'username' => Html::a($data->user->name, ['users/view', 'id' => $data->user_id, 'slug' => $data->user->slug]),
                        'question' => Html::a($data->answer->question->title, ['questions/view', 'id' => $data->answer->question_id, 'slug' => $data->answer->question->slug])
                    ]
                );
                $notification->date = date('Y-m-d H:i:s');
                // TBD - check for failures
                $notification->save();
            }
        break;

        case 'qcomment':
            if($data->question->user_id != $data->user_id) {
                $notification = new UserNotifications;
                $notification->user_id = $data->question->user_id;
                $notification->type = UserNotifications::TYPE_QUESTION_COMMENT;
                $notification->details = Yii::t(
                    'app', 
                    $notification->msgs[$type], [
                        'username' => Html::a($data->user->name, ['users/view', 'id' => $data->user_id, 'slug' => $data->user->slug]),
                        'question' => Html::a($data->question->title, ['questions/view', 'id' => $data->question_id, 'slug' => $data->question->slug])
                    ]
                );
                $notification->date = date('Y-m-d H:i:s');
                // TBD - check for failures
                $notification->save();
            }
        break;

        case 'message':
            if($data->to_user != $data->from_user) {
                $notification = new UserNotifications;
                $notification->user_id = $data->to_user;
                $notification->type = UserNotifications::TYPE_MESSAGE;
                $notification->details = Yii::t(
                    'app', 
                    $notification->msgs[$type], [
                        'username' => Html::a($data->fromUser->name, ['users/view', 'id' => $data->from_user, 'slug' => $data->fromUser->slug]),
                        'message' => Html::a(Yii::t('app', 'message'), ['messages/thread', 'id' => $data->parent_id == null ? $data->id : $data->parent_id])
                    ]
                );
                $notification->date = date('Y-m-d H:i:s');
                // TBD - check for failures
                $notification->save();
            }
        break;
    }
}

This is a code via which notifications are saving. But nothing happening.

rob006
  • 18,710
  • 5
  • 41
  • 58
Shivam
  • 1
  • 1
    Have you tried `$notification->save(false);` – Ravi May 29 '18 at 10:48
  • Yes i tried, Found this on Google and applied, but not worked. :( – Shivam May 29 '18 at 10:49
  • check this link :- https://stackoverflow.com/questions/33759514/model-save-not-working-in-yii2 – Ravi May 29 '18 at 10:53
  • Checked but still nothing happen. Infact i am not even getting any error or var_dump thing. – Shivam May 29 '18 at 10:58
  • i can guess the `$type` parameter value, but what do you pass for the `$data` and, the above function looks to be saving via some other action or method, means you are calling this function to save the notification from some other function, if you are not able to save anything in the db and not getting any errors too against the `save()` you might have to double check if you are calling the above function inside a `transaction` block? – Reborn May 29 '18 at 17:59
  • also if you can add the `UserNotifications` model to see if there is any `beforeSave()` you are overiding? – Reborn May 29 '18 at 18:03

2 Answers2

0

save() will not even try to save record into database if validation fail. You should start from checking result of save() call and potential errors:

if (!$notification->save()) {
   var_dump($notification->getErrors();
}

Alternatively you may skip validation, but this can be dangerous if you're not sure that data is safe.

$notification->save(false);
rob006
  • 18,710
  • 5
  • 41
  • 58
-1

A best practice before trying to save a record is validate the model like this:

if($notification->validate()){
  $notification->save();

  // make a log or another action
}else{
  // here you can debbug
  var_dump($notification->getErrors());

  // make a log or another action
}

If after this the model doesn't show any error, maybe you have a problem at a database level, like datatype or different length for some column

  • This is unnecessary complicated, since `save()` already internally calls `validate()` and returns `false` if validation dose not pass. – rob006 May 02 '21 at 20:20
  • @rob006 Here is the documentation: https://www.yiiframework.com/doc/guide/2.0/es/input-validation it is more complex but is better because you can take different actions depending if there is an error in the validate() process or in the save() process. – Jose Manuel Kun May 04 '21 at 17:08
  • This documentation is for `Model`, which does not have `save()` method. For Active Record you call `save()` directly after loading data (like in https://www.yiiframework.com/doc/guide/2.0/en/structure-controllers#actions) to avoid running validation twice. – rob006 May 04 '21 at 17:17