common\services\NotificationService::batchNotify PHP Method

batchNotify() public method

批量处理通知
public batchNotify ( $type, User $fromUser, $users, Post $post, PostComment $comment = null ) : boolean
$type
$fromUser common\models\User
$users
$post common\models\Post
$comment common\models\PostComment
return boolean
    public function batchNotify($type, User $fromUser, $users, Post $post, PostComment $comment = null)
    {
        foreach ($users as $key => $value) {
            if ($fromUser->id == $key) {
                continue;
            }
            $model = new Notification();
            $model->setAttributes(['from_user_id' => $fromUser->id, 'user_id' => $key, 'post_id' => $post->id, 'comment_id' => $comment ? $comment->id : 0, 'data' => $comment ? $comment->comment : $post->content, 'type' => $type]);
            $this->notifiedUsers[] = $key;
            if ($model->save()) {
                User::updateAllCounters(['notification_count' => 1], ['id' => $key]);
            } else {
                throw new Exception(array_values($model->getFirstErrors())[0]);
            }
        }
        return count($this->notifiedUsers);
    }