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. 10 core/tests/Drupal/KernelTests/Core/Database/InsertTest.php \Drupal\KernelTests\Core\Database\InsertTest::testInsertFieldOnlyDefinition()
  3. 11.x 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 119

Class

InsertTest
Tests the insert builder.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testInsertFieldOnlyDefinition() {
    // 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->assertIdentical($saved_age, '30', 'Can retrieve after inserting.');
    $saved_age = $this->connection
        ->query('SELECT age FROM {test} WHERE name = :name', [
        ':name' => 'Curly',
    ])
        ->fetchField();
    $this->assertIdentical($saved_age, '31', 'Can retrieve after inserting.');
    $saved_age = $this->connection
        ->query('SELECT age FROM {test} WHERE name = :name', [
        ':name' => 'Moe',
    ])
        ->fetchField();
    $this->assertIdentical($saved_age, '32', 'Can retrieve after inserting.');
}

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