You are here

public function InsertTest::testInsertFieldOnlyDefinition in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Database/InsertTest.php \Drupal\KernelTests\Core\Database\InsertTest::testInsertFieldOnlyDefinition()
  2. 9 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 121

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