You are here

function InsertTest::testInsertFieldOnlyDefinition in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Database/InsertTest.php \Drupal\system\Tests\Database\InsertTest::testInsertFieldOnlyDefinition()

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

File

core/modules/system/src/Tests/Database/InsertTest.php, line 107
Contains \Drupal\system\Tests\Database\InsertTest.

Class

InsertTest
Tests the insert builder.

Namespace

Drupal\system\Tests\Database

Code

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