You are here

function DatabaseInsertTestCase::testInsertFieldOnlyDefinintion in Drupal 7

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

File

modules/simpletest/tests/database_test.test, line 581

Class

DatabaseInsertTestCase
Test the insert builder.

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