The Query_Builder class is the parent class for all Query_Builder classes. It contains the main code for compiling and executing a query.
It also handles queries that are handcrafted, and constructed using DB::query.
The type method returns the type of query defined by the instance, as defined by the DB class
constants SELECT, INSERT, UPDATE or DELETE.
Static |
No |
Parameters |
None.
|
Returns |
Integer. The type of query this object defines. |
Example |
$query = \DB::query('DELETE * FROM table', \DB::DELETE);
if ($query->type() == \DB::DELETE)
{
}
|
If no type is given when constructing a query, or if the query is generated, this method will return null.
In that case, the type will be determined at execution time.
The cached method enables the result of the query to be cached for a specified amount of time.
If the exact same query is executed again within the lifetime defined, the cached result is returned instead.
Static |
No |
Parameters |
Param |
Type |
Default |
Description |
$lifetime |
integer |
null |
number of seconds to cache or null for the default cache timeout. |
$cache_key |
string |
null |
name of the cache key to be used or null for an automatically generated cache key. |
$cache_all |
boolean |
true |
if true, all results will be cached, if false, only non-empty results will be cached. |
|
Returns |
Database_Query
|
Example |
$table = 'table';
$query = DB::query('SELECT * FROM :tablename', DB::SELECT);
$result = $query->as_object()->cached(60, 'AllFromTable', false)->execute();
|
When called, the result is return as an associative array, instead of as objects. This is the default.
Static |
No |
Parameters |
None
|
Returns |
Indexed array of assoc arrays
|
Example |
$table = 'table';
$query = DB::query('SELECT * FROM :tablename', DB::SELECT);
$result = $query->as_assoc()->execute();
foreach ($result as $row)
{
echo $row['id'];
}
|
When called, the result is return as an array of objects. If no argument is passed, of if the default value
true is passed, the rows in the result will be returned as instances of StdClass.
If you pass a class name, instances of that class will be returned instead.
This is particularly useful if you want a custom query to return instances of Model_Crud or an Orm Model. Note
that is these cases, the result MUST contain the complete primary key!
Static |
No |
Parameters |
Param |
Type |
Default |
Description |
$class |
string |
true |
The name of the class to be used for result objects. |
|
Returns |
Indexed array of objects
|
Example |
$table = 'table';
$query = DB::query('SELECT * FROM :tablename', DB::SELECT);
$result = $query->as_object()->execute();
foreach ($result as $row)
{
echo $row->id;
}
$result = $query->as_object('Model_Tablename')->execute();
|
The set_connection method allows you to execute the query against a specific database connection.
If not specified, the query runs against the connection defined as "default" in your database configuration. It is
mainly used to be able to dynamically swap the database used to run the query, independent of the code that actually
executes the query.
Static |
No |
Parameters |
Param |
Type |
Default |
Description |
$db |
string |
null |
The database connection name. |
|
Returns |
Database_Query
|
Example |
$table = 'table';
$query = DB::query('DELETE * FROM :tablename', DB::DELETE);
$query->param('tablename', $table);
$query->set_connection('2nd-db');
$result = $query->execute();
$result = $query->execute('2nd-db');
|
The compile method will compile the SQL defined within the Query object, and return it. It will
return the SQL in the dialect defined by the database connection passed, the database connection set, or the default
connection. The SQL dialect used will depend of the driver the connection defines.
Static |
No |
Parameters |
Param |
Type |
Default |
Description |
$db |
string |
null |
The database connection name. |
|
Returns |
string, the compiled SQL statement
|
Example |
$table = 'table';
$query = DB::query('DELETE * FROM :tablename', DB::DELETE);
$query->param('tablename', $table);
$sql = $query->compile();
|
The execute method will executed the compiled SQL as defined within the Query object, and return its
results. It will use the connection passed, the connection set, or the default connection to find the driver to
execute it, in this order.
Static |
No |
Parameters |
Param |
Type |
Default |
Description |
$db |
string |
null |
The database connection name. |
|
Returns |
Database_Query_Result for SELECT type queries
Database_Query_Cached for SELECT type query results that are returned from cache
The insert id for INSERT type queries (if an auto_increment key is used)
The number of affected rows for UPDATE type queries
|
Example |
$table = 'table';
$query = DB::query('DELETE * FROM :tablename', DB::DELETE);
$query->param('tablename', $table);
$query->execute();
|
When you want to code SQL queries by hand, it is strongly advised not to work with string concatenations, but use parameters
and parameter replacement instead. It will keep your code much cleaner and more secure, your queries easier to update, and
the values in your query easier to change or update. For examples see the usage page.
$table = 'table';
$id = 123;
$name = 'John';
$query = DB::query('SELECT * FROM '.$table.'. WHERE id = '.$id.' AND name = "'.$name.'"');
$query = DB::query('SELECT * FROM :tablename WHERE id = :id AND name = :name');
$query->parameters(array(
'tablename' => $table,
'id' => $id,
'name' => $name
});
Using parameter replacement makes sure your variables are properly quoted, preventing SQL injection.
The param method allows you to set the value of a parameter in the query.
Static |
No |
Parameters |
Param |
Type |
Default |
Description |
$param |
string |
required |
the parameter name |
$value |
string |
required |
the value to assign to the parameter |
|
Returns |
Database_Query |
Example |
$table = 'table';
$query = DB::query('DELETE * FROM :tablename', DB::DELETE);
$query->param('tablename', $table);
$table = 'newtable';
$sql = $query->compile();
|
The parameters method allows you to set multiple parameter values in the query.
Static |
No |
Parameters |
Param |
Type |
Default |
Description |
$params |
array |
required |
assoc array of parameter/value combinations |
|
Returns |
Database_Query |
Example |
$table = 'table';
$query = DB::query('DELETE * FROM :tablename WHERE `id` = :id', DB::DELETE);
$query->parameters(array('tablename' => $table, 'id' => 1));
$sql = $query->compile();
|
The bind method allows you to bind a parameter to a variable in your code.
Static |
No |
Parameters |
Param |
Type |
Default |
Description |
$param |
string |
required |
the parameter name |
$var |
string |
required |
the variable to bind the parameter to |
|
Returns |
Database_Query |
Example |
$table = 'tablename';
$query = DB::query('DELETE * FROM :tablename', DB::DELETE);
$query->bind('tablename', $table);
$table = 'newtable';
$sql = $query->compile();
|