class Insert extends Query implements Countable (View source)

General class for an abstracted INSERT query.

Traits

Provides common functionality for INSERT and UPSERT queries.

Properties

protected Connection $connection

The connection object on which to run this query.

from  Query
protected string $connectionTarget

The target of the connection object.

from  Query
protected string $connectionKey

The key of the connection object.

from  Query
protected array $queryOptions

The query options to pass on to the connection object.

from  Query
protected string $uniqueIdentifier

A unique identifier for this query object.

from  Query
protected int $nextPlaceholder

The placeholder counter.

from  Query
protected array $comments

An array of comments that can be prepended to a query.

from  Query
protected string $table

The table on which to insert.

from  InsertTrait
protected array $insertFields

An array of fields on which to insert.

from  InsertTrait
protected array $defaultFields

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

from  InsertTrait
protected array $insertValues

A nested array of values to insert.

from  InsertTrait
protected SelectInterface $fromQuery

A SelectQuery object to fetch the rows that should be inserted.

Methods

__construct(Connection $connection, string $table, array $options = [])

Constructs an Insert object.

__sleep()

Implements the magic __sleep function to disconnect from the database.

from  Query
__wakeup()

Implements the magic __wakeup function to reconnect to the database.

from  Query
__clone()

Implements the magic __clone function.

from  Query
execute()

Executes the insert query.

string
__toString()

Implements PHP magic __toString method to convert the query to a string.

uniqueIdentifier()

Returns a unique identifier for this object.

from  Query
The
nextPlaceholder()

Gets the next placeholder value for this query object.

from  Query
$this
comment($comment)

Adds a comment to the query.

from  Query
array
getComments()

Returns a reference to the comments array for the query.

from  Query
getConnection()

Gets the database connection to be used for the query.

from  Query
$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}

$this
from(SelectInterface $query)

Sets the fromQuery on this InsertQuery object.

bool
preExecute()

Preprocesses and validates the query.

Details

__construct(Connection $connection, string $table, array $options = [])

Constructs an Insert object.

Parameters

Connection $connection

Database connection object.

string $table

Name of the table to associate with this query.

array $options

Array of query options.

__sleep()

Implements the magic __sleep function to disconnect from the database.

__wakeup()

Implements the magic __wakeup function to reconnect to the database.

__clone()

Implements the magic __clone function.

StatementInterface|null execute()

Executes the insert query.

Return Value

StatementInterface|null

A prepared statement, or NULL if the query is not valid.

string __toString()

Implements PHP magic __toString method to convert the query to a string.

Return Value

string

A prepared statement query string for this object.

uniqueIdentifier()

Returns a unique identifier for this object.

The nextPlaceholder()

Gets the next placeholder value for this query object.

Return Value

The

next available placeholder ID as an integer.

$this comment($comment)

Adds a comment to the query.

By adding a comment to a query, you can more easily find it in your query log or the list of active queries on an SQL server. This allows for easier debugging and allows you to more easily find where a query with a performance problem is being generated.

The comment string will be sanitized to remove * / and other characters that may terminate the string early so as to avoid SQL injection attacks.

Parameters

$comment

The comment string to be inserted into the query.

Return Value

$this

array getComments()

Returns a reference to the comments array for the query.

Because this method returns by reference, alter hooks may edit the comments array directly to make their changes. If just adding comments, however, the use of comment() is preferred.

Note that this method must be called by reference as well:

Return Value

array

A reference to the comments array structure.

Connection getConnection()

Gets the database connection to be used for the query.

Return Value

Connection

The database connection to be used for the query.

$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}

$this from(SelectInterface $query)

Sets the fromQuery on this InsertQuery object.

Parameters

SelectInterface $query

The query to fetch the rows that should be inserted.

Return Value

$this

The called object.

protected bool preExecute()

Preprocesses and validates the query.

Return Value

bool

TRUE if the validation was successful, FALSE if not.

Exceptions

FieldsOverlapException
NoFieldsException