You are here

public function PrivateMessageMapper::getThreadIdForMembers in Private Message 8

Same name and namespace in other branches
  1. 8.2 src/Mapper/PrivateMessageMapper.php \Drupal\private_message\Mapper\PrivateMessageMapper::getThreadIdForMembers()

Retrieve the ID of a thread from the database.

The thread returned will contain all of the given UIDs, and only the given UIDs.

Parameters

array $uids: An array of User IDs of users whose thread should be retrieved.

Return value

int|bool If a thread is found, the thread ID will be returned. Otherwise FALSE will be returned.

Overrides PrivateMessageMapperInterface::getThreadIdForMembers

File

src/Mapper/PrivateMessageMapper.php, line 45

Class

PrivateMessageMapper
Interface for the Private Message Mapper class.

Namespace

Drupal\private_message\Mapper

Code

public function getThreadIdForMembers(array $members) {
  $uids = [];
  foreach ($members as $member) {
    $uids[] = $member
      ->id();
  }
  $query = $this->database
    ->select('private_message_threads', 'pm')
    ->fields('pm', [
    'id',
  ])
    ->range(0, 1);

  // First do an inner join for each user, to ensure that the user exists in
  // the theread.
  $i = 0;
  foreach ($uids as $uid) {
    $tmp_alias = 'member_' . $i;
    $query
      ->join('private_message_thread__members', $tmp_alias, $tmp_alias . '.entity_id = pm.id AND ' . $tmp_alias . '.members_target_id = :uid' . $i, [
      ':uid' . $i => $uid,
    ]);
    $i++;
  }

  // Next, do a left join for all rows that don't contain the users, and
  // ensure that there aren't any additional users in selected threads.
  $alias = $query
    ->leftJoin('private_message_thread__members', 'member', 'member.entity_id = pm.id AND member.members_target_id NOT IN (:uids[])', [
    ':uids[]' => $uids,
  ]);
  $query
    ->isNull($alias . '.members_target_id');
  return $query
    ->execute()
    ->fetchField();
}