Use the static insert
method to create an INSERT
statement builder.
use WTFramework\SQL\SQL;
$stmt = SQL::insert();
Use the into
method to set the table name.
$stmt->into('t1');
Use the column
method to specify the column(s).
$stmt->column('c1');
$stmt->column(['c2', 'c3']);
Use the values
method to set the values. The values should be ordered according to the table column order, or the order of the columns as passed to the column
method.
$stmt->values([1, 2, 3]);
If the value is a string then it will be treated as a bound parameter. You may use SQL::raw
to avoid this.
$stmt->where([SQL::raw('DEFAULT'), 2, 3]);
Pass an array of arrays to insert multiple rows.
$stmt->values([
[1, 2, 3],
[4, 5, 6],
]);
Use the set
method to explicity set the value of a column. Pass as the first argument the column and the second argument the value.
$stmt->set('c1', 1);
You may also pass an array of values with the column name as the key.
$stmt->set([
'c1' => 1,
'c2' => 2,
]);
Use the select
method to insert the resultset of a SELECT
statement.
$stmt->select(SQL::select()->from('t2'));
Use the rowAlias
method to set the row alias.
$stmt->rowAlias('new');
You may also pass column aliases as the second argument.
$stmt->rowAlias('new', ['a', 'b', 'c']);
Use the onDuplicateKeyUpdate
method to explicity set the value of a column where a matching row already exists. Pass as the first argument the column and the second argument the value.
$stmt->onDuplicateKeyUpdate('c1', 1);
You may also pass an array of values with the column name as the key.
$stmt->onDuplicateKeyUpdate([
'c1' => 1,
'c2' => 2,
]);
Use the onConflict
method to specify an action to perform if a matching row already exists.
$stmt->onConflict('DO NOTHING');
An Upsert
service class can also be passed, providing a fluent interface for generating upserts.
$stmt->onConflict(SQL::upsert());
Use the returning
method to return the inserted column(s). If called with no arguments it will return *
.
$stmt->returning();
$stmt->returning('c1');
$stmt->returning(['c2', 'c3']);
$stmt->lowPriority();
$stmt->delayed();
$stmt->highPriority();
$stmt->ignore();
$stmt->overridingSystemValue();
$stmt->overridingUserValue();
$stmt->orReplace();
$stmt->orFail();
$stmt->orIgnore();
$stmt->orRollBack();
See Select documentation.
See Select documentation.
See Select documentation.
See Select documentation.
See Select documentation.