trait InsertTrait (View source)

Provides common functionality for INSERT and UPSERT queries.

Properties

protected string $table

The table on which to insert.

protected array $insertFields

An array of fields on which to insert.

protected array $defaultFields

An array of fields that should be set to their database-defined defaults.

protected array $insertValues

A nested array of values to insert.

Methods

$this
fields(array $fields, array $values = [])

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

$this
values(array $values)

Adds another set of values to the query to be inserted.

$this
useDefaults(array $fields)

Specifies fields for which the database defaults should be used.

array
getInsertPlaceholderFragment(array $nested_insert_values, array $default_fields)

Returns the query placeholders for values that will be inserted.

count()

{@inheritdoc}

Details

$this fields(array $fields, array $values = [])

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.

$this values(array $values)

Adds another set of values to the query to be inserted.

If $values is a numeric-keyed array, it will be assumed to be in the same order as the original fields() call. If it is associative, it may be in any order as long as the keys of the array match the names of the fields.

Parameters

array $values

An array of values to add to the query.

Return Value

$this

The called object.

$this useDefaults(array $fields)

Specifies fields for which the database defaults should be used.

If you want to force a given field to use the database-defined default, not NULL or undefined, use this method to instruct the database to use default values explicitly. In most cases this will not be necessary unless you are inserting a row that is all default values, as you cannot specify no values in an INSERT query.

Specifying a field both in fields() and in useDefaults() is an error and will not execute.

Parameters

array $fields

An array of values for which to use the default values specified in the table definition.

Return Value

$this

The called object.

protected array getInsertPlaceholderFragment(array $nested_insert_values, array $default_fields)

Returns the query placeholders for values that will be inserted.

Parameters

array $nested_insert_values

A nested array of values to insert.

array $default_fields

An array of fields that should be set to their database-defined defaults.

Return Value

array

An array of insert placeholders.

count()

{@inheritdoc}