You are here

public function DbtngExampleRepository::load in Examples for Developers 8

Same name and namespace in other branches
  1. 3.x modules/dbtng_example/src/DbtngExampleRepository.php \Drupal\dbtng_example\DbtngExampleRepository::load()

Read from the database using a filter array.

The standard function to perform reads for static queries is Connection::query().

Connection::query() uses 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.

The following is a query which uses a string literal SQL query. The placeholders will be substituted with the values in the array. Placeholders are marked with a colon ':'. Table names are marked with braces, so that Drupal's' multisite feature can add prefixes as needed.

// SELECT * FROM {dbtng_example} WHERE uid = 0 AND name = 'John'
\Drupal::database()
  ->query("SELECT * FROM {dbtng_example} WHERE uid = :uid and name = :name", [
  ':uid' => 0,
  ':name' => 'John',
])
  ->execute();

For more dynamic queries, Drupal provides Connection::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'
\Drupal::database()
  ->select('dbtng_example')
  ->fields('dbtng_example')
  ->condition('uid', 0)
  ->condition('name', 'John')
  ->execute();

Here is select() with named placeholders:

// SELECT * FROM {dbtng_example} WHERE uid = 0 AND name = 'John'
$arguments = array(
  ':name' => 'John',
  ':uid' => 0,
);
\Drupal::database()
  ->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
\Drupal::database()
  ->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

Drupal\Core\Database\Connection::select()

File

dbtng_example/src/DbtngExampleRepository.php, line 199

Class

DbtngExampleRepository
Repository for database-related helper methods for our example.

Namespace

Drupal\dbtng_example

Code

public function load(array $entry = []) {

  // Read all the fields from the dbtng_example table.
  $select = $this->connection
    ->select('dbtng_example')
    ->fields('dbtng_example');

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