You are here

public function PartyQuery::joinField in Party 8.2

Same name and namespace in other branches
  1. 7 includes/party.extender.inc \PartyQuery::joinField()

Create a join to an field on an attached entity.

Parameters

$data_set string|array: Either a data set name or a data set array.

$field string|array: Either a field name or a field info array.

$field_delta int: Optionally provide a delta to join to for the field.

$data_set_delta int: Optionally provide a delta to join to for the data set.

$type string: The type of join. Typically one of INNER, LEFT OUTER and RIGHT OUTER. Defaults to INNER.

Return value

string The table alias for the attached entity.

1 call to PartyQuery::joinField()
PartyQuery::fieldCondition in includes/party.extender.inc
Set a simple property condition on an attached entity.

File

includes/party.extender.inc, line 180
Class to aid querying parties and attached entities.

Class

PartyQuery
Query extender for party attached entities.

Code

public function joinField($data_set, $field, $alias = NULL, $field_delta = NULL, $data_set_delta = NULL, $type = 'INNER') {

  // Get hold of our data set definition.
  if (is_scalar($data_set)) {
    if ($data_set == 'party') {
      $data_set = array(
        'set_name' => 'party',
        'entity type' => 'party',
      );
    }
    else {
      $data_set_definition = party_get_data_set_info($data_set);
      if (empty($data_set_definition)) {
        throw new Exception(t('Unknown data set: @data_set', array(
          '@data_set' => $data_set,
        )));
      }
      $data_set = $data_set_definition;
    }
  }

  // Get hold of the field information.
  if (is_scalar($field)) {
    $field_definition = field_info_field($field);
    if (empty($field_definition)) {
      throw new Exception(t('Unknown field: @field_name', array(
        '@field_name' => $field,
      )));
    }
    $field = $field_definition;
  }
  if ($data_set['set_name'] == 'party') {

    // We don't need to join as we're on party.
    $entity_alias = $this->base_alias;
  }
  else {

    // Get our join to the entity table.
    $entity_alias = $this
      ->joinAttachedEntity($data_set, NULL, $data_set_delta, $type);
  }

  // Find or create our join to the field table.
  $field_key = implode(':', array(
    $data_set['set_name'],
    $data_set_delta,
    $field['field_name'],
    $field_delta,
    $type,
  ));
  if (!isset($this->fields[$field_key])) {
    $entity_info = entity_get_info($data_set['entity type']);
    $table_name = _field_sql_storage_tablename($field);

    // Build our conditions as an array which we'll implode with ANDs.
    $conditions = array(
      "%alias.entity_type = :entity_type",
      "%alias.entity_id = {$entity_alias}.{$entity_info['entity keys']['id']}",
    );

    // Build our arguments array.
    $arguments = array(
      ':entity_type' => $data_set['entity type'],
    );

    // If we're joining a specific delta, we need some additional conditions.
    if (isset($field_delta)) {
      $conditions[] = "{$entity_alias}.delta = :delta";
      $arguments[':delta'] = $field_delta;
    }

    // Create our join.
    $this->fields[$field_key] = $this
      ->addJoin($type, $table_name, $field['field_name'], implode(' AND ', $conditions), $arguments);
  }
  return $this->fields[$field_key];
}