function InsertTest::testInsertFieldOnlyDefinintion

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

File

drupal/core/modules/system/lib/Drupal/system/Tests/Database/InsertTest.php, line 113
Definition of Drupal\system\Tests\Database\InsertTest.

Class

InsertTest
Tests the insert builder.

Namespace

Drupal\system\Tests\Database

Code

function testInsertFieldOnlyDefinintion() {

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