public function InsertTest::testInsertSelectFields in Drupal 9
Same name and namespace in other branches
- 8 core/tests/Drupal/KernelTests/Core/Database/InsertTest.php \Drupal\KernelTests\Core\Database\InsertTest::testInsertSelectFields()
Tests that the INSERT INTO ... SELECT (fields) ... syntax works.
File
- core/
tests/ Drupal/ KernelTests/ Core/ Database/ InsertTest.php, line 155
Class
- InsertTest
- Tests the insert builder.
Namespace
Drupal\KernelTests\Core\DatabaseCode
public function testInsertSelectFields() {
$query = $this->connection
->select('test_people', 'tp');
// The query builder will always append expressions after fields.
// Add the expression first to test that the insert fields are correctly
// re-ordered.
$query
->addExpression('[tp].[age]', 'age');
$query
->fields('tp', [
'name',
'job',
])
->condition('tp.name', 'Meredith');
// The resulting query should be equivalent to:
// INSERT INTO test (age, name, job)
// SELECT tp.age AS age, tp.name AS name, tp.job AS job
// FROM test_people tp
// WHERE tp.name = 'Meredith'
$this->connection
->insert('test')
->from($query)
->execute();
$saved_age = $this->connection
->query('SELECT [age] FROM {test} WHERE [name] = :name', [
':name' => 'Meredith',
])
->fetchField();
$this
->assertSame('30', $saved_age, 'Can retrieve after inserting.');
}