protected function DrupalDefaultEntityController::buildQuery
7.x entity.inc | protected DrupalDefaultEntityController::buildQuery($ids, $conditions = array(), $revision_id = FALSE) |
Builds the query to load the entity.
This has full revision support. For entities requiring special queries, the class can be extended, and the default query can be constructed by calling parent::buildQuery(). This is usually necessary when the object being loaded needs to be augmented with additional data from another table, such as loading node type into comments or vocabulary machine name into terms, however it can also support $conditions on different tables. See CommentController::buildQuery() or TaxonomyTermController::buildQuery() for examples.
Parameters
$ids: An array of entity IDs, or FALSE to load all entities.
$conditions: An array of conditions in the form 'field' => $value.
$revision_id: The ID of the revision to load, or FALSE if this query is asking for the most current revision(s).
Return value
SelectQuery A SelectQuery object for loading the entity.
- CommentController::buildQuery in drupal-7.x/
modules/ comment/ comment.module - Builds the query to load the entity.
- DrupalDefaultEntityController::load in drupal-7.x/
includes/ entity.inc - Implements DrupalEntityControllerInterface::load().
- NodeController::buildQuery in drupal-7.x/
modules/ node/ node.module - Builds the query to load the entity.
- TaxonomyTermController::buildQuery in drupal-7.x/
modules/ taxonomy/ taxonomy.module - Builds the query to load the entity.
- TaxonomyVocabularyController::buildQuery in drupal-7.x/
modules/ taxonomy/ taxonomy.module - Builds the query to load the entity.
- CommentController::buildQuery in drupal-7.x/
modules/ comment/ comment.module - Builds the query to load the entity.
- NodeController::buildQuery in drupal-7.x/
modules/ node/ node.module - Builds the query to load the entity.
- TaxonomyTermController::buildQuery in drupal-7.x/
modules/ taxonomy/ taxonomy.module - Builds the query to load the entity.
- TaxonomyVocabularyController::buildQuery in drupal-7.x/
modules/ taxonomy/ taxonomy.module - Builds the query to load the entity.
File
- drupal-7.x/
includes/ entity.inc, line 247
Class
- DrupalDefaultEntityController
- Default implementation of DrupalEntityControllerInterface.
Code
protected function buildQuery($ids, $conditions = array(), $revision_id = FALSE) {
$query = db_select($this->entityInfo['base table'], 'base');
$query->addTag($this->entityType . '_load_multiple');
if ($revision_id) {
$query->join($this->revisionTable, 'revision', "revision.{$this->idKey} = base.{$this->idKey} AND revision.{$this->revisionKey} = :revisionId", array(':revisionId' => $revision_id));
}
elseif ($this->revisionKey) {
$query->join($this->revisionTable, 'revision', "revision.{$this->revisionKey} = base.{$this->revisionKey}");
}
// Add fields from the {entity} table.
$entity_fields = $this->entityInfo['schema_fields_sql']['base table'];
if ($this->revisionKey) {
// Add all fields from the {entity_revision} table.
$entity_revision_fields = drupal_map_assoc($this->entityInfo['schema_fields_sql']['revision table']);
// The id field is provided by entity, so remove it.
unset($entity_revision_fields[$this->idKey]);
// Remove all fields from the base table that are also fields by the same
// name in the revision table.
$entity_field_keys = array_flip($entity_fields);
foreach ($entity_revision_fields as $key => $name) {
if (isset($entity_field_keys[$name])) {
unset($entity_fields[$entity_field_keys[$name]]);
}
}
$query->fields('revision', $entity_revision_fields);
}
$query->fields('base', $entity_fields);
if ($ids) {
$query->condition("base.{$this->idKey}", $ids, 'IN');
}
if ($conditions) {
foreach ($conditions as $field => $value) {
$query->condition('base.' . $field, $value);
}
}
return $query;
}