function InsertTest::testInsertSelectAll

Same name in other branches
  1. 8.9.x core/tests/Drupal/KernelTests/Core/Database/InsertTest.php \Drupal\KernelTests\Core\Database\InsertTest::testInsertSelectAll()
  2. 10 core/tests/Drupal/KernelTests/Core/Database/InsertTest.php \Drupal\KernelTests\Core\Database\InsertTest::testInsertSelectAll()
  3. 11.x core/tests/Drupal/KernelTests/Core/Database/InsertTest.php \Drupal\KernelTests\Core\Database\InsertTest::testInsertSelectAll()

Tests that the INSERT INTO ... SELECT * ... syntax works.

File

core/tests/Drupal/KernelTests/Core/Database/InsertTest.php, line 181

Class

InsertTest
Tests the insert builder.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testInsertSelectAll() {
    $query = $this->connection
        ->select('test_people', 'tp')
        ->fields('tp')
        ->condition('tp.name', 'Meredith');
    // The resulting query should be equivalent to:
    // INSERT INTO test_people_copy
    // SELECT *
    // FROM test_people tp
    // WHERE tp.name = 'Meredith'
    $this->connection
        ->insert('test_people_copy')
        ->from($query)
        ->execute();
    $saved_age = $this->connection
        ->query('SELECT [age] FROM {test_people_copy} WHERE [name] = :name', [
        ':name' => 'Meredith',
    ])
        ->fetchField();
    $this->assertSame('30', $saved_age, 'Can retrieve after inserting.');
}

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