You are here

protected function ServicesEntityResourceController::propertyQueryOperation in Services Entity API 7.2

Helper function for adding a property to an EntityFieldQuery.

This takes care of distinguishing between fields and entity properties when adding a condition or ordering to an EntityFieldQuery. It executes the right EntityFieldQuery method to add the property to the query.

Parameters

string $entity_type: The entity type for the query.

EntityFieldQuery $query: The EntityFieldQuery object.

string $operation: The general method name, without the words 'property' or 'field'. E.g., one of 'Condition' or 'OrderBy'.

string $property: The name of the raw property or field which is to be added to the query.

string|array $value: The value for the function.

2 calls to ServicesEntityResourceController::propertyQueryOperation()
ServicesEntityResourceController::index in plugins/services_entity_resource.inc
Implements ServicesResourceControllerInterface::index().
ServicesEntityResourceControllerClean::propertyQueryOperation in plugins/services_entity_resource_clean.inc
Overridden to translate metadata property name to schema field.
1 method overrides ServicesEntityResourceController::propertyQueryOperation()
ServicesEntityResourceControllerClean::propertyQueryOperation in plugins/services_entity_resource_clean.inc
Overridden to translate metadata property name to schema field.

File

plugins/services_entity_resource.inc, line 240

Class

ServicesEntityResourceController
Generic controller for entity-bases resources.

Code

protected function propertyQueryOperation($entity_type, EntityFieldQuery $query, $operation, $property, $value) {

  // First pass: check the entity's table schema.
  // Get the database schema for the entity's table.
  $entity_info = entity_get_info($entity_type);
  $schema = drupal_get_schema($entity_info['base table']);
  if (isset($schema['fields'][$property])) {

    // If the property is defined in the schema, use the schema property.
    // The EFQ method is either 'propertyCondition' or 'OrderByCondition'.
    $operation = 'property' . $operation;
    $query
      ->{$operation}($property, $value);
    return;
  }

  // Second pass: check fields.
  // Get the metadata property info for the entity type, including properties
  // for all bundles.
  $properties = entity_get_all_property_info($entity_type);
  if (isset($properties[$property]) && !empty($properties[$property]['field'])) {

    // For fields we need the field info to get the right column for the
    // query.
    $field_info = field_info_field($property);
    $operation = 'field' . $operation;
    if (is_array($value)) {

      // Specific column filters are given, so add a query condition for each
      // one of them.
      foreach ($value as $column => $val) {
        $query
          ->{$operation}($field_info, $column, $val);
      }
    }
    else {

      // Just pick the first field column for the operation.
      $columns = array_keys($field_info['columns']);
      $column = $columns[0];
      $query
        ->{$operation}($field_info, $column, $value);
    }
    return;
  }

  // Still here if no matching property was found.
  services_error(t('Parameter @prop does not exist', array(
    '@prop' => $property,
  )), 406);
}