7.x entity.inc DrupalDefaultEntityController::load($ids = array(), $conditions = array())

Implements DrupalEntityControllerInterface::load().

Overrides DrupalEntityControllerInterface::load


includes/entity.inc, line 157


Default implementation of DrupalEntityControllerInterface.


public function load($ids = array(), $conditions = array()) {
  $entities = array();

  // Revisions are not statically cached, and require a different query to
  // other conditions, so separate the revision id into its own variable.
  if ($this->revisionKey && isset($conditions[$this->revisionKey])) {
    $revision_id = $conditions[$this->revisionKey];
  else {
    $revision_id = FALSE;

  // Create a new variable which is either a prepared version of the $ids
  // array for later comparison with the entity cache, or FALSE if no $ids
  // were passed. The $ids array is reduced as items are loaded from cache,
  // and we need to know if it's empty for this reason to avoid querying the
  // database when all requested entities are loaded from cache.
  $passed_ids = !empty($ids) ? array_flip($ids) : FALSE;

  // Try to load entities from the static cache, if the entity type supports
  // static caching.
  if ($this->cache && !$revision_id) {
    $entities += $this
      ->cacheGet($ids, $conditions);

    // If any entities were loaded, remove them from the ids still to load.
    if ($passed_ids) {
      $ids = array_keys(array_diff_key($passed_ids, $entities));

  // Ensure integer entity IDs are valid.
  if (!empty($ids)) {

  // Load any remaining entities from the database. This is the case if $ids
  // is set to FALSE (so we load all entities), if there are any ids left to
  // load, if loading a revision, or if $conditions was passed without $ids.
  if ($ids === FALSE || $ids || $revision_id || $conditions && !$passed_ids) {

    // Build the query.
    $query = $this
      ->buildQuery($ids, $conditions, $revision_id);
    $queried_entities = $query

  // Pass all entities loaded from the database through $this->attachLoad(),
  // which attaches fields (if supported by the entity type) and calls the
  // entity type specific load callback, for example hook_node_load().
  if (!empty($queried_entities)) {
      ->attachLoad($queried_entities, $revision_id);
    $entities += $queried_entities;
  if ($this->cache) {

    // Add entities to the cache if we are not loading a revision.
    if (!empty($queried_entities) && !$revision_id) {

  // Ensure that the returned array is ordered the same as the original
  // $ids array if this was passed in and remove any invalid ids.
  if ($passed_ids) {

    // Remove any invalid ids from the array.
    $passed_ids = array_intersect_key($passed_ids, $entities);
    foreach ($entities as $entity) {
      $passed_ids[$entity->{$this->idKey}] = $entity;
    $entities = $passed_ids;
  return $entities;