select("{$thChatBase}.id") ->joinWith('chatUsers') ->andWhere([ "{$thChatBase}.id" => $chatId, "{$tnChatUser}.archive" => false, ]) ->andWhere(['!=', "{$tnChatUser}.user_id", $userId]); return static::find() ->joinWith('chatUsers') ->andWhere([ "{$thChatBase}.id" => $chatId, "{$tnChatUser}.user_id" => $userId, "{$tnChatUser}.archive" => false, ]) ->andWhere(['NOT IN', "{$thChatBase}.id", $subQuery]); } public static function findChatByUserId(int $chatId, int $userId): ?ChatPersonToEmpty { return static::findChatByUserIdQuery($chatId, $userId)->one(); } public static function isExists(int $chatId, array $userIds): bool { $userIdsWithAbiturientRole = ChatPersonToEmpty::getUserIdsWithAbiturientRole($userIds); return static::findChatByUserIdQuery($chatId, $userIdsWithAbiturientRole)->exists(); } public static function getUserIdsWithAbiturientRole(array $userIds): int { return (int) RBACAuthAssignment::find() ->select('user_id') ->andWhere(['IN', 'user_id', $userIds]) ->andWhere(['item_name' => User::ROLE_ABITURIENT]) ->scalar(); } }