You are here

function SelectSubqueryTest::testExistsSubquerySelect in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/system/src/Tests/Database/SelectSubqueryTest.php \Drupal\system\Tests\Database\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

core/modules/system/src/Tests/Database/SelectSubqueryTest.php, line 130
Contains \Drupal\system\Tests\Database\SelectSubqueryTest.

Class

SelectSubqueryTest
Tests the Select query builder.

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.');
}