Query_Builder_Update class
(extends Query_Builder_Where)
The Query_Builder_Update class handles all the update 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 update.
Static |
No |
Parameters |
Param |
Type |
Default |
Description |
$table |
string |
required |
the table name |
|
Returns |
Returns the current instance. |
Example |
// prepare an update statement
$query = DB::update('users');
// Set the table to update
$query->table('admins');
// UPDATE `admins` ...
|
value($column, $value)
The value method sets a column and value to update.
Static |
No |
Parameters |
Param |
Type |
Default |
Description |
$column |
string |
required |
the column to update |
$value |
mixed |
required |
the new value |
|
Returns |
Returns the current instance. |
Example |
// prepare an update statement
$query = DB::update('users');
// Set the columns
$query->value('name', 'Frank');
// UPDATE `users` SET `name` = "Frank"
|
set(array $pairs)
The set method sets the columns and values to update.
Static |
No |
Parameters |
Param |
Type |
Default |
Description |
$pairs |
array |
required |
associative array of columns and values |
|
Returns |
Returns the current instance. |
Example |
// prepare an update statement
$query = DB::update('users');
// Set the columns and vales
$query->set(array(
'name' => 'John',
'surname' => 'Doe',
));
// UPDATE `users` SET `name` = "John", `surname` = "Doe"
|
compile(\Database_Connection$db)
The compile method returns the update 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 update statement
$query = DB::update('users');
// Set two values
$query->set(array(
'name' => 'Bert',
'surname' => 'Klaassen',
));
// 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 update statement
$query = DB::update('users');
// Set two values
$query->set(array(
'name' => 'Bert',
'surname' => 'Klaassen',
));
// Reset it
$query->reset();
// Just set one
$query->value('name', 'Hank');
// Get the database connection
$connection = Database_Connection::instance();
// Get the sql query
$sql = $query->compile($connection);
// UPDATE `users` SET `name` = "Hank"
|
join($table, $type = null)
The join method appends tables to join.
Static |
No |
Parameters |
Param |
Type |
Default |
Description |
$table |
mixed |
required |
table name or array($table, $alias) |
$type |
mixed |
true
|
join type (LEFT, RIGHT, INNER, etc) |
|
Returns |
Returns the current instance. |
Example |
View example here
|
on($c1, $op, $c2)
The on method adds "ON ..." conditions for the last created JOIN statement.
Static |
No |
Parameters |
Param |
Type |
Default |
Description |
$c1 |
mixed |
required |
table name or array($table, $alias) |
$op |
string |
required |
logical operator |
$c2 |
mixed |
required |
table name or array($table, $alias) |
|
Returns |
Returns the current instance. |
Example |
// prepare an update statement
$query = DB::update('users');
// Join a table
$query->join('profiles');
$query->on('users.id', '=', 'profiles.user_id');
// Use a Database_Expression so the value does not get escaped.
$query->value('users.profile_type', \DB::expr('`profiles`.`type`'));
// UPDATE `users` JOIN `profiles` ON `users`.`id` = `profiles`.`user_id` SET `users`.`profile_type` = `profiles`.`type`
|
and_on()
The and_on method is an alias for on.
or_ on($c1, $op, $c2)
The on method adds "OR ON ..." conditions for the last created JOIN statement.
Static |
No |
Parameters |
Param |
Type |
Default |
Description |
$c1 |
mixed |
required |
table name or array($table, $alias) |
$op |
string |
required |
logical operator |
$c2 |
mixed |
required |
table name or array($table, $alias) |
|
Returns |
Returns the current instance. |
Example |
// prepare an update statement
$query = DB::update('users');
// Join a table
$query->join('profiles');
$query->on('users.id', '=', 'profiles.user_id');
// Add an OR ON condition
$query->or_on('users.id', '=', 'profiles.other_id');
// Use a Database_Expression so the value does not get escaped.
$query->value('users.profile_type', \DB::expr('`profiles`.`type`'));
// UPDATE `users` JOIN `profiles` ON `users`.`id` = `profiles`.`user_id` OR `users`.`id` = `profiles`.`other_id` SET `users`.`profile_type` = `profiles`.`type`
|
on_open()
The on_open method adds an open bracket for the last created JOIN statement.
Static |
No |
Parameters |
None
|
Returns |
Returns the current instance. |
Example |
See below
|
on_close()
The on_close method adds an closing bracket for the last created JOIN statement.
Static |
No |
Parameters |
None
|
Returns |
Returns the current instance. |
Example |
// prepare a select statement
$query = DB::update('users');
// Join a table
$query->join('profiles');
$query->on('users.id', '=', 'profiles.user_id');
// Add some nested conditions
$query->on_open()
->on('profiles.status', '=', DB::expr('A')
->or_on('profiles.status', '=', DB::expr('B')
->on_close();
// Use a Database_Expression so the value does not get escaped.
$query->value('users.profile_type', \DB::expr('`profiles`.`type`'));
// UPDATE `users` JOIN `profiles` ON `users`.`id` = `profiles`.`user_id`
// AND (`profiles`.`status` = "A" OR `profiles`.`status` = "B"))
// SET `users`.`profile_type` = `profiles`.`type`
|
and_on()
The and_on method is an alias for on.