You are here

public function InsertTrait::fields in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Database/Query/InsertTrait.php \Drupal\Core\Database\Query\InsertTrait::fields()

Adds a set of field->value pairs to be inserted.

This method may only be called once. Calling it a second time will be ignored. To queue up multiple sets of values to be inserted at once, use the values() method.

Parameters

array $fields: An array of fields on which to insert. This array may be indexed or associative. If indexed, the array is taken to be the list of fields. If associative, the keys of the array are taken to be the fields and the values are taken to be corresponding values to insert. If a $values argument is provided, $fields must be indexed.

array $values: (optional) An array of fields to insert into the database. The values must be specified in the same order as the $fields array.

Return value

$this The called object.

1 call to InsertTrait::fields()
Insert::preExecute in core/lib/Drupal/Core/Database/Query/Insert.php
Preprocesses and validates the query.

File

core/lib/Drupal/Core/Database/Query/InsertTrait.php, line 70

Class

InsertTrait
Provides common functionality for INSERT and UPSERT queries.

Namespace

Drupal\Core\Database\Query

Code

public function fields(array $fields, array $values = []) {
  if (empty($this->insertFields)) {
    if (empty($values)) {
      if (!is_numeric(key($fields))) {
        $values = array_values($fields);
        $fields = array_keys($fields);
      }
    }
    $this->insertFields = $fields;
    if (!empty($values)) {
      $this->insertValues[] = $values;
    }
  }
  return $this;
}