Skip to main content
edited body
Source Link
Berdir
  • 83.5k
  • 6
  • 161
  • 207

There's nothing different from 7.x with db_query(), it returns a result set where you can fetch results from or loop over it. Refer to the database API documentation.

However, there are even less reasons for doing a direct database query in 8.x than in 7.x, and doing the query by hand is very likely going to be more complicated because of the data tables.

Use entity query instead:

<?php
$uids = \Drupal::entityQuery('user')
  ->execute();
?>

Executing an entity query always returns entity IDSIDs which can then be loaded:

<?php
$users = \Drupal::entityManager()->getStorage('user')->loadMultiple($uids);
// Shorthand if you know that you have users:
$users = \Drupal\user\Entity\User::loadMultiple($uids);
?>

There's nothing different from 7.x with db_query(), it returns a result set where you can fetch results from or loop over it. Refer to the database API documentation.

However, there are even less reasons for doing a direct database query in 8.x than in 7.x, and doing the query by hand is very likely going to be more complicated because of the data tables.

Use entity query instead:

<?php
$uids = \Drupal::entityQuery('user')
  ->execute();
?>

Executing an entity query always returns entity IDS which can then be loaded:

<?php
$users = \Drupal::entityManager()->getStorage('user')->loadMultiple($uids);
// Shorthand if you know that you have users:
$users = \Drupal\user\Entity\User::loadMultiple($uids);
?>

There's nothing different from 7.x with db_query(), it returns a result set where you can fetch results from or loop over it. Refer to the database API documentation.

However, there are even less reasons for doing a direct database query in 8.x than in 7.x, and doing the query by hand is very likely going to be more complicated because of the data tables.

Use entity query instead:

<?php
$uids = \Drupal::entityQuery('user')
  ->execute();
?>

Executing an entity query always returns entity IDs which can then be loaded:

<?php
$users = \Drupal::entityManager()->getStorage('user')->loadMultiple($uids);
// Shorthand if you know that you have users:
$users = \Drupal\user\Entity\User::loadMultiple($uids);
?>
Source Link
Berdir
  • 83.5k
  • 6
  • 161
  • 207

There's nothing different from 7.x with db_query(), it returns a result set where you can fetch results from or loop over it. Refer to the database API documentation.

However, there are even less reasons for doing a direct database query in 8.x than in 7.x, and doing the query by hand is very likely going to be more complicated because of the data tables.

Use entity query instead:

<?php
$uids = \Drupal::entityQuery('user')
  ->execute();
?>

Executing an entity query always returns entity IDS which can then be loaded:

<?php
$users = \Drupal::entityManager()->getStorage('user')->loadMultiple($uids);
// Shorthand if you know that you have users:
$users = \Drupal\user\Entity\User::loadMultiple($uids);
?>