You are here

public static function CartStorage::load in Basic cart 8.0

Same name and namespace in other branches
  1. 8.6 src/CartStorage.php \Drupal\basic_cart\CartStorage::load()
  2. 8 src/CartStorage.php \Drupal\basic_cart\CartStorage::load()
  3. 8.2 src/CartStorage.php \Drupal\basic_cart\CartStorage::load()
  4. 8.3 src/CartStorage.php \Drupal\basic_cart\CartStorage::load()
  5. 8.4 src/CartStorage.php \Drupal\basic_cart\CartStorage::load()
  6. 8.5 src/CartStorage.php \Drupal\basic_cart\CartStorage::load()

Read from the database using a filter array.

The standard function to perform reads was db_query(), and for static queries, it still is.

db_query() used an SQL query with placeholders and arguments as parameters.

Drupal DBTNG provides an abstracted interface that will work with a wide variety of database engines.

db_query() is deprecated except when doing a static query. The following is perfectly acceptable in Drupal 8. See the handbook page on static queries

// SELECT * FROM {dbtng_example} WHERE uid = 0 AND name = 'John'
db_query("SELECT * FROM {dbtng_example} WHERE uid = :uid and name = :name", array(
  ':uid' => 0,
  ':name' => 'John',
))
  ->execute();

But for more dynamic queries, Drupal provides the db_select() API method, so there are several ways to perform the same SQL query. See the handbook page on dynamic queries.

// SELECT * FROM {dbtng_example} WHERE uid = 0 AND name = 'John'
db_select('dbtng_example')
  ->fields('dbtng_example')
  ->condition('uid', 0)
  ->condition('name', 'John')
  ->execute();

Here is db_select with named placeholders:

// SELECT * FROM {dbtng_example} WHERE uid = 0 AND name = 'John'
$arguments = array(
  ':name' => 'John',
  ':uid' => 0,
);
db_select('dbtng_example')
  ->fields('dbtng_example')
  ->where('uid = :uid AND name = :name', $arguments)
  ->execute();

Conditions are stacked and evaluated as AND and OR depending on the type of query. For more information, read the conditional queries handbook page at: http://drupal.org/node/310086

The condition argument is an 'equal' evaluation by default, but this can be altered:

// SELECT * FROM {dbtng_example} WHERE age > 18
db_select('dbtng_example')
  ->fields('dbtng_example')
  ->condition('age', 18, '>')
  ->execute();

Parameters

array $entry: An array containing all the fields used to search the entries in the table.

Return value

object An object containing the loaded entries if found.

See also

db_select()

db_query()

http://drupal.org/node/310072

http://drupal.org/node/310075

File

src/CartStorage.php, line 174

Class

CartStorage
Class CartStorage.

Namespace

Drupal\basic_cart

Code

public static function load($entry = array()) {

  // Read all fields from the dbtng_example table.
  $select = db_select(self::TABLE, 'cart');
  $select
    ->fields('cart');

  // Add each field and value as a condition to this query.
  foreach ($entry as $field => $value) {
    $select
      ->condition($field, $value);
  }

  // Return the result in object format.
  return $select
    ->execute()
    ->fetchAll();
}