You are here

class PartyQuery in Party 7

Same name and namespace in other branches
  1. 8.2 includes/party.extender.inc \PartyQuery

Query extender for party attached entities.

This allows you to extend select query to hadd helper methods for querying attached entities.

To respect party access permissions, add the 'party_access' tag to the query using SelectQuery::addTag().

Hierarchy

Expanded class hierarchy of PartyQuery

1 string reference to 'PartyQuery'
party_query in ./party.module
Returns a new SelectQuery extended by PartyQuery for the active database.

File

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

View source
class PartyQuery extends SelectQueryExtender {

  /**
   * The alias of the base party table to build from. Defaults to party.
   */
  protected $base_alias = 'party';

  /**
   * An array of entity table aliases keyed by data_set_name:delta:type that we
   * have joined to.
   *
   * Although this may result in duplicate joins to a single entitiy table,
   * that may be necessary if you are querying fields on the entity itself
   * across different data sets.
   *
   * @see PartyQuery::party_attached_entity.
   */
  protected $data_sets = array();

  /**
   * An array of field table aliases keyed by
   * data_set_name:data_set_delta:field_name:field_delta:type that we have
   * joined to.
   *
   * @see PartyQuery::party_attached_entity.
   */
  protected $fields = array();

  /**
   * Attempt to sniff out the party base table.
   */
  public function __construct(SelectQueryInterface $query, DatabaseConnection $connection) {
    parent::__construct($query, $connection);
    $tables = $this
      ->getTables();
    foreach ($tables as $alias => $table) {
      if ($table['table'] == 'party') {
        $this->base_alias = $alias;
        break;
      }
    }
  }

  /**
   * Get or set the alias for the base party table.
   *
   * @param string $alias
   *   Optionally a string containing the base party table alias. If FALSE
   *   we return the alias without changing it.
   *
   * @return string
   *   The alias for the base party table.
   */
  public function alias($alias = FALSE) {
    if ($alias) {
      $this->base_alias = $alias;
    }
    return $alias;
  }

  /**
   * Create a join to an attached entity.
   *
   * @param $data_set string|array
   *   Either a data set name or a data set array.
   * @param $delta int
   *   Optionally provide a delta to join to.
   * @param $type string
   *   The type of join. Typically one of INNER, LEFT OUTER and RIGHT OUTER.
   *   Defaults to INNER.
   *
   * @return string
   *   The table alias for the attached entity.
   */
  public function joinAttachedEntity($data_set, $alias = NULL, $delta = NULL, $type = 'INNER') {

    // Get hold of our data set definition.
    if (is_scalar($data_set)) {
      $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;
    }

    // Find or create our join to the entity table.
    $data_set_key = implode(':', array(
      $data_set['set_name'],
      $delta,
      $type,
    ));
    if (!isset($alias)) {
      $alias = $data_set['set_name'];
    }

    // Find or create our join.
    if (!isset($this->data_sets[$data_set_key])) {

      // Start with the party_attached_entity table.
      $data_set_placeholder = ':data_set_' . $this
        ->nextPlaceholder();
      $conditions = "%alias.pid = {$this->base_alias}.pid AND %alias.data_set = {$data_set_placeholder}";
      $arguments = array(
        $data_set_placeholder => $data_set['set_name'],
      );

      // If we're joining a specific delta, we need some additional conditions.
      if (isset($delta)) {
        $delta_placeholder = ':delta_' . $this
          ->nextPlaceholder();
        $conditions .= " AND %alias.delta = {$delta_placeholder}";
        $arguments[$delta_placeholder] = $delta;
      }
      $pae_alias = $this
        ->addJoin($type, 'party_attached_entity', 'pae', $conditions, $arguments);

      // Get hold of our entity info for building our join.
      $entity_info = entity_get_info($data_set['entity type']);
      if (empty($entity_info)) {
        throw new Exception(t('Unknown entity: @entity_type', array(
          '@entity_type' => $data_set['entity type'],
        )));
      }

      // Create our join.
      $this->data_sets[$data_set_key] = $this
        ->addJoin($type, $entity_info['base table'], $alias, "%alias.{$entity_info['entity keys']['id']} = {$pae_alias}.eid");
    }
    return $this->data_sets[$data_set_key];
  }

  /**
   * Create a join to an field on an attached entity.
   *
   * @param $data_set string|array
   *   Either a data set name or a data set array.
   * @param $field string|array
   *   Either a field name or a field info array.
   * @param $field_delta int
   *   Optionally provide a delta to join to for the field.
   * @param $data_set_delta int
   *   Optionally provide a delta to join to for the data set.
   * @param $type string
   *   The type of join. Typically one of INNER, LEFT OUTER and RIGHT OUTER.
   *   Defaults to INNER.
   *
   * @return string
   *   The table alias for the attached entity.
   */
  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 (!isset($alias)) {
      $alias = $field['field_name'];
    }
    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.
      $entity_type_placeholder = ':entity_type_' . $this
        ->nextPlaceholder();
      $conditions = array(
        "%alias.entity_type = {$entity_type_placeholder}",
        "%alias.entity_id = {$entity_alias}.{$entity_info['entity keys']['id']}",
      );

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

      // If we're joining a specific delta, we need some additional conditions.
      if (isset($field_delta)) {
        $delta_placeholder = ':delta_' . $this
          ->nextPlaceholder();
        $conditions[] = "%alias.delta = {$delta_placeholder}";
        $arguments[$delta_placeholder] = $field_delta;
      }

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

  /**
   * Set a simple property condition on an attached entity.
   *
   * This doesn't handle more complicated condition sets. If you need that
   * functionality, use PartyQuery::joinAttachedEntity() and set your
   * conditions using the normal SelectQuery methods.
   *
   * @param $data_set
   *   Either a data set name or a data set array.
   * @param $column
   *   A column defined in the hook_schema() of the base table of the entity.
   * @param $value
   *   The value to test the field against. In most cases, this is a scalar. For
   *   more complex options, it is an array. The meaning of each element in the
   *   array is dependent on $operator.
   * @param $operator
   *   Possible values:
   *   - '=', '<>', '>', '>=', '<', '<=', 'STARTS_WITH', 'CONTAINS': These
   *     operators expect $value to be a literal of the same type as the
   *     column.
   *   - 'IN', 'NOT IN': These operators expect $value to be an array of
   *     literals of the same type as the column.
   *   - 'BETWEEN': This operator expects $value to be an array of two literals
   *     of the same type as the column.
   *   The operator can be omitted, and will default to 'IN' if the value is an
   *   array, or to '=' otherwise.
   * @param $delta
   *   Optionally specify an attached entity delta you want to query against.
   * @param $type string
   *   The type of join. Typically one of INNER, LEFT OUTER and RIGHT OUTER.
   *   Defaults to INNER.
   *
   * @see PartyQuery::joinAttachedEntity().
   *
   * @return PartyQuery
   *  The build PartyQuery.
   */
  public function propertyCondition($data_set, $column, $value, $operator = NULL, $delta = NULL, $type = 'INNER') {

    // Get our entity table alias.
    $entity_alias = $this
      ->joinAttachedEntity($data_set, NULL, $delta, $type);

    // Set our condition.
    $this
      ->condition("{$entity_alias}.{$column}", $value, $operator);
    return $this;
  }

  /**
   * Set a simple property condition on an attached entity.
   *
   * This doesn't handle more complicated condition sets. If you need that
   * functionality, use PartyQuery::joinAttachedEntity() and set your
   * conditions using the normal SelectQuery methods.
   *
   * @param $data_set
   *   Either a data set name or a data set array.
   * @param $field string|array
   *   Either a field name or a field info array.
   * @param $column
   *   A column defined in the hook_field_schema() of this field. If this is
   *   omitted then the query will find only entities that have data in this
   *   field, using the entity and property conditions if there are any.
   * @param $value
   *   The value to test the field against. In most cases, this is a scalar. For
   *   more complex options, it is an array. The meaning of each element in the
   *   array is dependent on $operator.
   * @param $operator
   *   Possible values:
   *   - '=', '<>', '>', '>=', '<', '<=', 'STARTS_WITH', 'CONTAINS': These
   *     operators expect $value to be a literal of the same type as the
   *     column.
   *   - 'IN', 'NOT IN': These operators expect $value to be an array of
   *     literals of the same type as the column.
   *   - 'BETWEEN': This operator expects $value to be an array of two literals
   *     of the same type as the column.
   *   The operator can be omitted, and will default to 'IN' if the value is an
   *   array, or to '=' otherwise.
   * @param $field_delta
   *   Optionally specify an field delta you want to query against.
   * @param $data_set_delta
   *   Optionally specify an attached entity delta you want to query against.
   * @param $type string
   *   The type of join. Typically one of INNER, LEFT OUTER and RIGHT OUTER.
   *   Defaults to INNER.
   *
   * @see PartyQuery::joinAttachedEntity().
   *
   * @return PartyQuery
   *  The build PartyQuery.
   */
  public function fieldCondition($data_set, $field, $column, $value, $operator = NULL, $field_delta = NULL, $data_set_delta = NULL, $type = 'INNER') {

    // 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;
    }

    // Get our entity table alias.
    $field_alias = $this
      ->joinField($data_set, $field, NULL, $field_delta, $data_set_delta, $type);

    // Set our condition.
    $this
      ->condition("{$field_alias}.{$field['field_name']}_{$column}", $value, $operator);
    return $this;
  }

  /**
   * Execute a special case where we want a list of party ids.
   *
   * This is a shortcut method to select the party id, group by party id to
   * remove any duplicates, execute and return an array of pids.
   *
   * @return array
   *   An array of party pids.
   */
  public function fetchPids() {

    // Make sure we're not selecting all fields from any tables.
    $tables =& $this
      ->getTables();
    foreach ($tables as &$table) {
      $table['all_fields'] = FALSE;
    }

    // Make sure we only select party.pid.
    $fields =& $this
      ->getFields();
    $fields = array(
      'pid' => array(
        'field' => 'pid',
        'table' => $this->base_alias,
        'alias' => 'pid',
      ),
    );

    // Make sure we only group by party.pid.
    $group_by =& $this
      ->getGroupBy();
    $group_by = drupal_map_assoc(array(
      "{$this->base_alias}.pid",
    ));

    // Execute and return our pids.
    $result = $this
      ->execute();
    return $result
      ->fetchCol();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PartyQuery::$base_alias protected property The alias of the base party table to build from. Defaults to party.
PartyQuery::$data_sets protected property An array of entity table aliases keyed by data_set_name:delta:type that we have joined to.
PartyQuery::$fields protected property An array of field table aliases keyed by data_set_name:data_set_delta:field_name:field_delta:type that we have joined to.
PartyQuery::alias public function Get or set the alias for the base party table.
PartyQuery::fetchPids public function Execute a special case where we want a list of party ids.
PartyQuery::fieldCondition public function Set a simple property condition on an attached entity.
PartyQuery::joinAttachedEntity public function Create a join to an attached entity.
PartyQuery::joinField public function Create a join to an field on an attached entity.
PartyQuery::propertyCondition public function Set a simple property condition on an attached entity.
PartyQuery::__construct public function Attempt to sniff out the party base table. Overrides SelectQueryExtender::__construct
SelectQueryExtender::$connection protected property The connection object on which to run this query.
SelectQueryExtender::$placeholder protected property The placeholder counter.
SelectQueryExtender::$query protected property The SelectQuery object we are extending/decorating.
SelectQueryExtender::$uniqueIdentifier protected property A unique identifier for this query object.
SelectQueryExtender::addExpression public function Adds an expression to the list of "fields" to be SELECTed. Overrides SelectQueryInterface::addExpression
SelectQueryExtender::addField public function Adds a field to the list to be SELECTed. Overrides SelectQueryInterface::addField
SelectQueryExtender::addJoin public function Join against another table in the database. Overrides SelectQueryInterface::addJoin
SelectQueryExtender::addMetaData public function Adds additional metadata to the query. Overrides QueryAlterableInterface::addMetaData
SelectQueryExtender::addTag public function Adds a tag to a query. Overrides QueryAlterableInterface::addTag
SelectQueryExtender::arguments public function Gets a complete list of all values to insert into the prepared statement. Overrides QueryConditionInterface::arguments
SelectQueryExtender::compile public function Compiles the saved conditions for later retrieval. Overrides QueryConditionInterface::compile
SelectQueryExtender::compiled public function Check whether a condition has been previously compiled. Overrides QueryConditionInterface::compiled
SelectQueryExtender::condition public function Helper function: builds the most common conditional clauses. Overrides QueryConditionInterface::condition
SelectQueryExtender::conditions public function Gets a complete list of all conditions in this conditional clause. Overrides QueryConditionInterface::conditions
SelectQueryExtender::countQuery public function Get the equivalent COUNT query of this query as a new query object. Overrides SelectQueryInterface::countQuery 1
SelectQueryExtender::distinct public function Sets this query to be DISTINCT. Overrides SelectQueryInterface::distinct
SelectQueryExtender::execute public function 2
SelectQueryExtender::exists public function Sets a condition that the specified subquery returns values. Overrides QueryConditionInterface::exists
SelectQueryExtender::extend public function Enhance this object by wrapping it in an extender object. Overrides QueryExtendableInterface::extend
SelectQueryExtender::fields public function Add multiple fields from the same table to be SELECTed. Overrides SelectQueryInterface::fields
SelectQueryExtender::forUpdate public function Add FOR UPDATE to the query. Overrides SelectQueryInterface::forUpdate
SelectQueryExtender::getArguments public function Compiles and returns an associative array of the arguments for this prepared statement. Overrides SelectQueryInterface::getArguments
SelectQueryExtender::getExpressions public function Returns a reference to the expressions array for this query. Overrides SelectQueryInterface::getExpressions
SelectQueryExtender::getFields public function Returns a reference to the fields array for this query. Overrides SelectQueryInterface::getFields
SelectQueryExtender::getGroupBy public function Returns a reference to the group-by array for this query. Overrides SelectQueryInterface::getGroupBy
SelectQueryExtender::getMetaData public function Retrieves a given piece of metadata. Overrides QueryAlterableInterface::getMetaData
SelectQueryExtender::getOrderBy public function Returns a reference to the order by array for this query. Overrides SelectQueryInterface::getOrderBy
SelectQueryExtender::getTables public function Returns a reference to the tables array for this query. Overrides SelectQueryInterface::getTables
SelectQueryExtender::getUnion public function Returns a reference to the union queries for this query. This include queries for UNION, UNION ALL, and UNION DISTINCT. Overrides SelectQueryInterface::getUnion
SelectQueryExtender::groupBy public function Groups the result set by the specified field. Overrides SelectQueryInterface::groupBy
SelectQueryExtender::hasAllTags public function Determines if a given query has all specified tags. Overrides QueryAlterableInterface::hasAllTags
SelectQueryExtender::hasAnyTag public function Determines if a given query has any specified tag. Overrides QueryAlterableInterface::hasAnyTag
SelectQueryExtender::hasTag public function Determines if a given query has a given tag. Overrides QueryAlterableInterface::hasTag
SelectQueryExtender::having public function
SelectQueryExtender::havingArguments public function
SelectQueryExtender::havingCompile public function
SelectQueryExtender::havingCondition public function Helper function to build most common HAVING conditional clauses. Overrides SelectQueryInterface::havingCondition
SelectQueryExtender::havingConditions public function
SelectQueryExtender::innerJoin public function Inner Join against another table in the database. Overrides SelectQueryInterface::innerJoin
SelectQueryExtender::isNotNull function Sets a condition that the specified field be NOT NULL. Overrides QueryConditionInterface::isNotNull
SelectQueryExtender::isNull function Sets a condition that the specified field be NULL. Overrides QueryConditionInterface::isNull
SelectQueryExtender::isPrepared public function Indicates if preExecute() has already been called on that object. Overrides SelectQueryInterface::isPrepared
SelectQueryExtender::join public function Default Join against another table in the database. Overrides SelectQueryInterface::join
SelectQueryExtender::leftJoin public function Left Outer Join against another table in the database. Overrides SelectQueryInterface::leftJoin
SelectQueryExtender::nextPlaceholder public function Implements QueryPlaceholderInterface::nextPlaceholder(). Overrides QueryPlaceholderInterface::nextPlaceholder
SelectQueryExtender::notExists public function Sets a condition that the specified subquery returns no values. Overrides QueryConditionInterface::notExists
SelectQueryExtender::orderBy public function Orders the result set by a given field. Overrides SelectQueryInterface::orderBy
SelectQueryExtender::orderRandom public function Orders the result set by a random value. Overrides SelectQueryInterface::orderRandom
SelectQueryExtender::preExecute public function Generic preparation and validation for a SELECT query. Overrides SelectQueryInterface::preExecute
SelectQueryExtender::range public function Restricts a query to a given range in the result set. Overrides SelectQueryInterface::range
SelectQueryExtender::rightJoin public function Right Outer Join against another table in the database. Overrides SelectQueryInterface::rightJoin
SelectQueryExtender::union public function Add another Select query to UNION to this one. Overrides SelectQueryInterface::union
SelectQueryExtender::uniqueIdentifier public function Implements QueryPlaceholderInterface::uniqueIdentifier(). Overrides QueryPlaceholderInterface::uniqueIdentifier
SelectQueryExtender::where public function Adds an arbitrary WHERE clause to the query. Overrides QueryConditionInterface::where
SelectQueryExtender::__call public function Magic override for undefined methods.
SelectQueryExtender::__clone public function Clone magic method. Overrides SelectQueryInterface::__clone
SelectQueryExtender::__toString public function