function SelectSubqueryTest::testExistsSubquerySelect

Tests EXISTS subquery conditionals on SELECT statements.

We essentially select all rows from the {test} table that have matching rows in the {test_people} table based on the shared name column.

File

drupal/core/modules/system/lib/Drupal/system/Tests/Database/SelectSubqueryTest.php, line 136
Definition of Drupal\system\Tests\Database\SelectSubqueryTest.

Class

SelectSubqueryTest
Tests for subselects in a dynamic SELECT query.

Namespace

Drupal\system\Tests\Database

Code

function testExistsSubquerySelect() {

  // Put George into {test_people}.
  db_insert('test_people')
    ->fields(array(
    'name' => 'George',
    'age' => 27,
    'job' => 'Singer',
  ))
    ->execute();

  // Base query to {test}.
  $query = db_select('test', 't')
    ->fields('t', array(
    'name',
  ));

  // Subquery to {test_people}.
  $subquery = db_select('test_people', 'tp')
    ->fields('tp', array(
    'name',
  ))
    ->where('tp.name = t.name');
  $query
    ->exists($subquery);
  $result = $query
    ->execute();

  // Ensure that we got the right record.
  $record = $result
    ->fetch();
  $this
    ->assertEqual($record->name, 'George', 'Fetched name is correct using EXISTS query.');
}