获取所有用户的列表并放入数组


17

我想知道如何获取所有用户并将其所有UID放入数组中?我见过类似的问题,但没有人真正回答我的问题。

Answers:


18

在Drupal 7中,您可以使用entity_load获取所有用户,如下所示,

$users = entity_load('user');

和array_keys($ users)将给你uid的数组


9

您可以通过以下方法在Drupal 8中实现结果:

$query = \Drupal::entityQuery('user');

$uids = $query->execute();

1

只需共享我最近创建的代码段即可获取数据库中所有现有用户的HTML和EMAIL。

最多可支持2000多个用户。之后,我建议实现批处理或其他直接数据库查询。

这依赖于Drupal加载每个用户。

$query = new EntityFieldQuery();

$query->entityCondition('entity_type', 'user');
$html = '<table>';
$result = $query->execute();

$uids = array_keys($result['user']);

// THIS IS YOUR ARRAY OF UIDS.
$users = user_load_multiple($uids);

// EXTRA CODE.
foreach($users as $user){
  $mail = $user->mail;
  $name = $user->name;
  $html .= '<tr><td>'.$mail.'</td>'.'<td>'.$name.'</td></tr>';
}
$html .= '</table'>;

将返回一个简单的html表,您可以轻松将其复制到Excel等。

要将它们放入数组中,只需使用UIDS代码中声明的变量即可。


0

要回答一些评论:如果您正在与大量用户一起工作,您将无法在没有超时的情况下加载阵列中的所有用户,因此您可能需要批处理它们...

这是(是的:

 function _set_batch_revench_agains_users(){
  $batch = [
    'operations' => [
      ['_revench_against_users_batch_process']
    ],
    'finished' => '_revench_against_users_batch_finished',
    'title' => t('Punishing users for ungratefulness and general insolence.'),
    'init_message' => t('Batch is starting, he he he...'),
    'progress_message' => t('Processed @current out of @total.'),
    'error_message' => t('Batch has encountered an error, nooooo!'),
    'file' => drupal_get_path('module', 'revench_agains_users') . '/user_agains_users.module'
  ];
  batch_set($batch);
  batch_process('admin');
}

/**
 * Batch 'Processing' callback
 */

function _revench_against_users_batch_process(&$context){
 //all this $context stuff is mandatory, it is a bit heavy, but the batchAPI need it to keep track of progresses
  if (!isset($context['sandbox']['current'])) {
    $context['sandbox']['count'] = 0;
    $context['sandbox']['current'] = 0;
  }

  //don't use entity field query for such simple use cases as gettings all ids (much better performances, less code to write...)
  $query =  db_select('users', 'u');
  $query->addField('u', 'uid');
  $query->condition('u.uid', $context['sandbox']['current'], '>');
  $query->orderBy('u.uid');
  // Get the total amount of items to process.
  if (!isset($context['sandbox']['total'])) {
    $context['sandbox']['total'] = $query->countQuery()->execute()->fetchField();

    // If there are no users to "update", stop immediately.
    if (!$context['sandbox']['total']) {
      $context['finished'] = 1;
      return;
    }
  }

  $query->range(0, 25);
  $uids = $query->execute()->fetchCol();
  //finaly ! here is your user array, limited to a manageable chunk of 25 users
  $users_array = user_load_multiple($uids);
  //send it to some function to "process"...
  _burn_users_burnburnburn($users_array); // I won't reveal the content of this function for legal reasons
  //more mandatory context stuff
  $context['sandbox']['count'] += count($uids);
  $context['sandbox']['current'] = max($uids);
  $context['message'] = t('burned @uid ... feels better ...', array('@uid' => end($uids)));

  if ($context['sandbox']['count'] != $context['sandbox']['total']) {
    $context['finished'] = $context['sandbox']['count'] / $context['sandbox']['total'];
  }

}

/**
 * Batch 'finished' callback
 */
function _revench_against_users_batch_finished($success, $results, $operations) {
  if ($success) {
    // Here we do something meaningful with the results.
    $message = t('@count users successfully burned:', array('@count' => count($results)));

    drupal_set_message($message);
  }
  else {
    // An error occurred.
    // $operations contains the operations that remained unprocessed.
    $error_operation = reset($operations);
    $message = t('An error occurred while processing %error_operation with arguments: @arguments some users might have escaped !', array('%error_operation' => $error_operation[0], '@arguments' => print_r($error_operation[1], TRUE)));
    drupal_set_message($message, 'error');
  }
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.