Yii2 ActiveRecord :: save()不适用于通知

时间:2018-05-29 10:44:53

标签: yii2

我是Yii2及其控制器和型号的新手。我已安装了一个应用程序并正在运行,但通知未保存在数据库中。就像是用户评论或关注文章没有通知显示,因为数据没有存储到DB。

代码示例:

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;
    }
}

这是保存通知的代码。但没有任何事情发生。

2 个答案:

答案 0 :(得分:0)

如果验证失败,

save()甚至不会尝试将记录保存到数据库中。您应该从检查save()来电和潜在错误的结果开始:

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

或者您可以跳过验证,但如果您不确定数据是否安全,这可能会很危险。

$notification->save(false);

答案 1 :(得分:-1)

尝试保存记录之前的最佳实践是像这样验证模型:

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
}

如果此后模型没有显示任何错误,则可能您在数据库级别有问题,例如某些列的数据类型或长度不同