function InsertTest::testInsertFieldOnlyDefinition

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

Tests that we can specify fields without values and specify values later.

File

core/tests/Drupal/KernelTests/Core/Database/InsertTest.php, line 123

Class

InsertTest
Tests the insert builder.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testInsertFieldOnlyDefinition() : void {
    // This is useful for importers, when we want to create a query and define
    // its fields once, then loop over a multi-insert execution.
    $this->connection
        ->insert('test')
        ->fields([
        'name',
        'age',
    ])
        ->values([
        'Larry',
        '30',
    ])
        ->values([
        'Curly',
        '31',
    ])
        ->values([
        'Moe',
        '32',
    ])
        ->execute();
    $saved_age = $this->connection
        ->query('SELECT [age] FROM {test} WHERE [name] = :name', [
        ':name' => 'Larry',
    ])
        ->fetchField();
    $this->assertSame('30', $saved_age, 'Can retrieve after inserting.');
    $saved_age = $this->connection
        ->query('SELECT [age] FROM {test} WHERE [name] = :name', [
        ':name' => 'Curly',
    ])
        ->fetchField();
    $this->assertSame('31', $saved_age, 'Can retrieve after inserting.');
    $saved_age = $this->connection
        ->query('SELECT [age] FROM {test} WHERE [name] = :name', [
        ':name' => 'Moe',
    ])
        ->fetchField();
    $this->assertSame('32', $saved_age, 'Can retrieve after inserting.');
}

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