Query_Builder_Insert class

(extends Query_Builder_Where)

The Query_Builder_Insert class handles all the insert operations for the query building process. It extends the Query_Builder_Where class, so all the methods are inherited.

table($table)

The table method sets/changes the table to insert into.

Static No
Parameters
Param Type Default Description
$table string required the table name
Returns Returns the current instance.
Example
// prepare an insert statement
$query = DB::insert('users');

// Update the table to insert into
$query->table('admins');

// INSERT INTO `admins` ...

columns(array $columns)

The columns method sets/changes the columns to insert.

Static No
Parameters
Param Type Default Description
$columns array required an array of column names
Returns Returns the current instance.
Example
// prepare an insert statement
$query = DB::insert('users');

// Set the columns
$query->columns(array(
	'name',
	'surname',
	'email')
);

// INSERT INTO `users` (`name`, `surname`, `email`)

values(array $values)

The values method appends the values to insert.

Static No
Parameters
Param Type Default Description
$values array required list of values
Returns Returns the current instance.
Example
// prepare an insert statement
$query = DB::insert('users');

// Set the columns
$query->columns(array(
	'name',
	'surname',
	'email',
));

// Set the values

$query->values(array(
	'John',
	'Doe',
	'john@doe.com',
));

// INSERT INTO `users` (`name`, `surname`, `email`) VALUES ("John", "Doe", "john@doe.com")

set(array $pairs)

The set method sets the columns and appends the values to insert.

Static No
Parameters
Param Type Default Description
$pairs array required associative array of columns and values
Returns Returns the current instance.
Example
// prepare an insert statement
$query = DB::insert('users');

// Set the columns and values
$query->set(array(
	'name' => 'John',
	'surname' => 'Doe',
	'email' => 'john@doe.com',
));

// INSERT INTO `users` (`name`, `surname`, `email`) VALUES ("John", "Doe", "john@doe.com")

compile(\Database_Connection$db)

The limit method returns the insert SQL query as a string.

Static No
Parameters
Param Type Default Description
$db object required A database connection
Returns Returns the SQL query as a string.
Example
// prepare an insert statement
$query = DB::select('*')->from('users');

// Set the limit and offset
$query->limit(10);
$query->offset(5);

// Get the database connection
$connection = Database_Connection::instance();

// Get the sql query
$sql = $query->compile($connection);

reset()

The reset method resets all values of the current instance.

Static No
Parameters None
Returns Returns the current instance.
Example
// prepare an insert statement
$query = DB::insert('users');

// Set some values
$query->set(array(
	'name' => 'John',
	'surname' => 'Doe',
));

// Reset it
$query->reset();

// Set the new values
$query->table('admins')->set(array(
	'name' => 'Mark',
	'surname' => 'Handriks'
));

// The SQL query will now be:
// INSERT INTO `admins` (`name`, `surname`) VALUES ("Mark", "Hendriks")