You are here

function uc_attribute_load_multiple in Ubercart 8.4

Same name and namespace in other branches
  1. 6.2 uc_attribute/uc_attribute.module \uc_attribute_load_multiple()
  2. 7.3 uc_attribute/uc_attribute.module \uc_attribute_load_multiple()

Loads attribute objects from the database.

@todo If we feel it necessary, we could optimize this, by inverting the logic; that is, we could make uc_attribute load call this function and allow this function to minimize the number of queries necessary. -cha0s

Parameters

array $aids: Attribute IDs to load.

string $type: The type of attribute: 'product' or 'class'. Any other type will fetch a base attribute.

int $id: The ID of the product/class this attribute belongs to.

Return value

array An array of loaded attributes.

3 calls to uc_attribute_load_multiple()
AttributeTest::testAttributeApi in uc_attribute/tests/src/Functional/AttributeTest.php
Tests the basic attribute API.
CartLinksTest::createCartLinksProduct in uc_cart_links/src/Tests/CartLinksTest.php
Creates a product with all attribute types and options.
uc_attribute_load_product_attributes in uc_attribute/uc_attribute.module
Fetches an array of attribute objects that belong to a product.

File

uc_attribute/uc_attribute.module, line 448
Ubercart Attribute module.

Code

function uc_attribute_load_multiple(array $aids = [], $type = '', $id = NULL) {
  $sql = uc_attribute_type_info($type);

  // Product/class attributes.
  $connection = \Drupal::database();
  if (!empty($type)) {

    // Seems like a big query to get attribute IDs, but it's all about the sort.
    // (I'm not sure if the default ordering is propagating down correctly here.
    // It appears that product/class attributes with no ordering won't let the
    // attribute's propagate down, as it does when loading. -cha0s)
    $query = $connection
      ->select($sql['attr_table'], 'uca')
      ->fields('uca', [
      'aid',
    ])
      ->condition("uca.{$sql['id']}", $id);
    $query
      ->leftJoin('uc_attributes', 'ua', 'uca.aid = ua.aid');
    $query
      ->orderBy('uca.ordering')
      ->orderBy('ua.name');
  }
  else {
    $query = $connection
      ->select('uc_attributes', 'ua')
      ->fields('ua', [
      'aid',
    ])
      ->orderBy('ordering')
      ->orderBy('name');
  }

  // Filter by the attribute IDs requested.
  if (!empty($aids)) {

    // Sanity check - filter out non-numeric attribute IDs.
    $aids = array_filter($aids, 'is_numeric');
    $query
      ->condition('ua.aid', $aids, 'IN');
  }
  $aids = $query
    ->execute()
    ->fetchCol();

  // Load the attributes.
  $attributes = [];
  foreach ($aids as $aid) {
    $attributes[$aid] = uc_attribute_load($aid, $id, $type);
  }
  return $attributes;
}