function FetchTest::testQueryFetchCol

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Database/FetchTest.php \Drupal\KernelTests\Core\Database\FetchTest::testQueryFetchCol()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Database/FetchTest.php \Drupal\KernelTests\Core\Database\FetchTest::testQueryFetchCol()
  3. 10 core/tests/Drupal/KernelTests/Core/Database/FetchTest.php \Drupal\KernelTests\Core\Database\FetchTest::testQueryFetchCol()

Confirms that we can fetch an entire column of a result set at once.

File

core/tests/Drupal/KernelTests/Core/Database/FetchTest.php, line 152

Class

FetchTest
Tests the Database system's various fetch capabilities.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testQueryFetchCol() : void {
    $result = $this->connection
        ->query('SELECT [name] FROM {test} WHERE [age] > :age', [
        ':age' => 25,
    ]);
    $column = $result->fetchCol();
    $this->assertCount(3, $column, 'fetchCol() returns the right number of records.');
    $result = $this->connection
        ->query('SELECT [name] FROM {test} WHERE [age] > :age', [
        ':age' => 25,
    ]);
    $i = 0;
    foreach ($result as $record) {
        $this->assertSame($column[$i++], $record->name, 'Column matches direct access.');
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.