You are here

class SarniaController in Sarnia 7

Controller class for Sarnia entities.

This class extends the DrupalDefaultEntityController class, overriding the 'load' method for entities based on Solr documents.

Hierarchy

Expanded class hierarchy of SarniaController

2 string references to 'SarniaController'
sarnia_entity_info in ./sarnia.module
Implements hook_entity_info().
sarnia_entity_types in ./sarnia.module
Get information about all Sarnia-provided entities.

File

./sarnia.module, line 528

View source
class SarniaController extends DrupalDefaultEntityController {
  public function load($ids = array(), $conditions = array()) {

    // Get cached entities.
    $entities = $this
      ->cacheGet($ids, $conditions);

    // Load un-cached entities. In general, this will not need to load any
    // entities, since when Solr queries are run from Views,
    // SarniaSolrService::postQuery() has already stashed all of the loaded
    // entities.
    if ($ids === FALSE || ($load_ids = array_diff($ids, array_keys($entities)))) {

      // Load the Search API index to query.
      $index = search_api_index_load($this->entityInfo['bundles'][$this->entityType]['search_api_index']);

      // Disable facets when loading entities.
      $index
        ->server()
        ->disableFeature('search_api_facets');

      // Create a Search API query.
      $query = $index
        ->query(array(
        'parse mode' => 'direct',
        'sarnia use raw keys' => TRUE,
      ));

      // Result field containing the entity id.
      $id_field = $this->entityInfo['bundles'][$this->entityType]['id_field'];
      do {

        // Build the query string. We use search keys instead of filter queries
        // because they're cached differently by Solr. This loads items in
        // batches of 200.
        if (is_array($load_ids)) {
          $keys = array();
          $count = 0;
          foreach ($load_ids as $i => $id) {
            $keys[] = "{$id_field}:{$id}";
            unset($load_ids[$i]);
            $count++;
            if ($count >= 200) {
              break;
            }
          }
          $query
            ->keys(implode(' OR ', $keys));
        }

        // Run the query. We don't have to do anything beyond querying, since
        // SarniaSolrService::postQuery() stashes all entity results.
        // @todo check for entities that DON'T load, and cache those as FALSE.
        $query
          ->execute();
      } while (is_array($load_ids) && !empty($load_ids));

      // Retrieve entities from the cache, where ::stash() puts them after each
      // query.
      $entities = $this
        ->cacheGet($ids, $conditions);

      // Un-disable facets in case the server object is used again later.
      $index
        ->server()
        ->unDisableFeature('search_api_facets');
    }
    return $entities;
  }

  /**
   * Stash entities from a Solr response. This assumes that items in the
   * response contain all field data, and that all entities in the response
   * should be stashed.
   *
   * Called from SarniaSolrService::postQuery().
   *
   * @param array $results
   *   An array of results from Solr, keyed by entity id. Each result is an
   *   array of Solr properties.
   */
  public function stash($results) {
    $solr_document_field = '_data';
    foreach (current(field_info_instances($this->entityType)) as $field_name => $instance) {
      $field_info = field_info_field($field_name);
      if ($field_info['type'] == 'sarnia') {
        $solr_document_field = $field_name;
        break;
      }
    }
    $loaded_entities = array();
    foreach ($results as $entity_id => $result) {
      $loaded_entities[$entity_id] = (object) array(
        'type' => $this->entityType,
        $this->idKey => $entity_id,
        $solr_document_field => $result['fields'],
      );
    }

    // Load fields.
    if (!empty($loaded_entities)) {
      $this
        ->attachLoad($loaded_entities);
    }
    $this
      ->cacheSet($loaded_entities);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalDefaultEntityController::$cache protected property Whether this entity type should use the static cache.
DrupalDefaultEntityController::$entityCache protected property Static cache of entities, keyed by entity ID.
DrupalDefaultEntityController::$entityInfo protected property Array of information about the entity.
DrupalDefaultEntityController::$entityType protected property Entity type for this controller instance.
DrupalDefaultEntityController::$hookLoadArguments protected property Additional arguments to pass to hook_TYPE_load().
DrupalDefaultEntityController::$idKey protected property Name of the entity's ID field in the entity database table.
DrupalDefaultEntityController::$revisionKey protected property Name of entity's revision database table field, if it supports revisions.
DrupalDefaultEntityController::$revisionTable protected property The table that stores revisions, if the entity supports revisions.
DrupalDefaultEntityController::attachLoad protected function Attaches data to entities upon loading. 4
DrupalDefaultEntityController::buildQuery protected function Builds the query to load the entity. 4
DrupalDefaultEntityController::cacheGet protected function Gets entities from the static cache. 1
DrupalDefaultEntityController::cacheSet protected function Stores entities in the static entity cache.
DrupalDefaultEntityController::cleanIds protected function Ensures integer entity IDs are valid.
DrupalDefaultEntityController::filterId protected function Callback for array_filter that removes non-integer IDs.
DrupalDefaultEntityController::resetCache public function Implements DrupalEntityControllerInterface::resetCache(). Overrides DrupalEntityControllerInterface::resetCache
DrupalDefaultEntityController::__construct public function Constructor: sets basic variables.
SarniaController::load public function Implements DrupalEntityControllerInterface::load(). Overrides DrupalDefaultEntityController::load
SarniaController::stash public function Stash entities from a Solr response. This assumes that items in the response contain all field data, and that all entities in the response should be stashed.