This document describes how to generate and execute sql SQL queries using a query builder , (mostly the builder was modelled of off Laravel Query Builder, which already provides quite a thorough documentation , - however some methods might be a bit different). Any significant difference will be outlined in this document as a separate paragraph.
Location
ORM and its dependencies is are located under \core\orm namespace. The builder itself lives in \core\orm\query. Feel free to examine the code.
...
For the purpose of any examples in the given this document, the following tables will be used:
...
Returns a single row identified by passed idID. If the row does not exist in the table it returns null. It does ignore any other conditions, joins, unions, etc. and always fetches all columns for the row.
...
Returns only one row assuming that the row is unique with given parameters. Will throw an exception if multiple rows exist. You cannot use offset()
or limit()
in conjunction with one()
.
...
Other functions
value()
Returns the a single value from a record. This method will return the value of the column directly.
Section | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
...
Section | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Selects
Selecting a field
To select items from your database you should use select or add_select method, the first one overrides your sql SQL select statement, the latter one appends. Field names are passed through a standard regex check to help you against sql SQL injections.
Will give you the following result:
...
id
...
name
...
1
...
John Doe
Selecting aggregates
Sometimes you need to do more complex selects than just a field name, for example using aggregate functions, builder allows for it, treating it as valid column names. Most commonly aggregate functions are supported: MIN, MAX, AVG, SUM, COUNT.
Will give you the following result
...
Selecting subquery
It's possible to use subqueries in select statements as well
The above query should give you the following results:
...
If this is still not sufficient, then you might resort to the raw versions of the select methods:
Selecting raw statement
When you use select statement the field that you are attempting to select is validated against a regular expression "a-zA-Z-_|*" (it actually is a bit more complex to allow for aliases, aggregate functions and prefixes - more on that later). Sometimes though you might need to use more advanced SQL in select, to achieve it you might use raw methods, raw methods embed provided strings directly into sql statement. Browse Raw methods to see the list of all available raw methods.
Warning |
---|
Raw methods embed strings directly into sql query, sql injection protection is required |
...
Joins
Inner join
Query builder might join other tables for you. To perform an inner join (or just join for simplification) you may use the join method, the first argument is the name of the table to join, the remaining arguments define the column in the main table, the condition and the column in the table being joined.
Code Block | ||||
---|---|---|---|---|
| ||||
Code Block | ||||
| ||||
builder::table('users')
->select(['id', 'name']) // Select or add_select accept a column to select as well as array of columns
->first();
builder::table('users')
->select('id')
->add_select('name as name') // Adding as alias is supported as well and it will pass field validation check
->first();
builder::table('users')
->select('email')
->select('id') // This will override any previous select or add_select statements and replace it with "id"
->add_select('name') // This will append to "id" resulting in "id, name"
->first(); |
Will give you the following result:
id | name |
---|---|
1 | John Doe |
Code Block | ||||
---|---|---|---|---|
| ||||
builder::table('users')
->select('my field') // This will blow up and exception will be thrown here
->first(); |
Selecting aggregates
Sometimes you need to do more complex selects than just a field name, for example using aggregate functions, builder allows for it, treating it as valid column names. Most commonly aggregate functions are supported: MIN, MAX, AVG, SUM, COUNT.
Code Block | ||||
---|---|---|---|---|
| ||||
builder::table('orders') ->join('users', 'user_id ->select(['min(quantity) as min_quantity', '=', 'id'max(quantity) // Don't be alarmed that there is naming collision, the columns will be auto-prefixed automatically ->select('*', 'users.name as user_name') // By default nothing from a joined table will be selected though, you need to append that manually ->first(); // Same result as max_quantity']) ->add_select('avg(quantity) as avg_quantity') ->add_select('sum(quantity) as total_quantity') ->add_select('count(id) as orders') ->first(); |
Will give you the following result:
min_quantity | max_quantity | avg_quantity | total_quantity | orders |
---|---|---|---|---|
1 | 496 | 85 | 510 | 6 |
Selecting subquery
It's possible to use subqueries in select statements as well:
Code Block | ||||
---|---|---|---|---|
| ||||
builder::table('ordersusers') ->join('users ->select(['id', 'user_idname', 'id') // You can omit '=' condition in joins for convenience this will be equivalent to the code above ->select('*new subquery(function(builder $sub) { $sub->from('orders') ->select('max(quantity)') ->where('user_id', 'users.name as user_name') ->firstid'); })->as('max_order_quantity')) ->limit(2) ->get(); |
The above
...
query should give you the following
...
results:
iduser_id | name | goodsmax_itemorder_idquantity | user_name | 1|
---|---|---|---|---|
1 | 1 | 496 | John Doe |
Left join
To perform a left join use the following construct:
Right join
Full join
Cross join
Cross join is slightly different from all the previous joins as it does not take any arguments, only the table name
Joining multiple tables
You may join multiple tables using the builder, simply call join several times on the builder object.
Will give you the following result:
...
Complex condition joins
Sometimes you want to join tables on multiple conditions, this also possible via the builder, you would need to use the following syntax
Alternative syntax
To define a custom alias for a joined table, you may use an alternative syntax for table field:
Sub query join
Subquery joins are possible, you will need to pass a query builder instance or subquery class instance instead of a table name to the join method. An alias must be specified for subquery joins!
Where Clauses
Simple Where Clauses
You can use the where
method to add where conditions to your query. Where
accepts three arguments:
- Name of column
- The operator
[=, in, <, > , <> or !=, <=, >=, like, ilike, ...]
- The value to evaluate against the column
Where
provides a fluent interface and can be chained. By default the value will always end up as a named parameter, means it's protected against SQL injection.
...
language | php |
---|
...
496 | ||
2 | Jane Doe | 3 |
If this is still not sufficient, then you might resort to the raw versions of the select methods:
Selecting raw statement
When you use select statement the field that you are attempting to select is validated against a regular expression "a-zA-Z-_|*" (it actually is a bit more complex to allow for aliases, aggregate functions and prefixes - more on that later). Sometimes though you might need to use more advanced SQL in select, to achieve it you might use raw methods, raw methods embed provided strings directly into SQL statement. Browse Raw methods to see the list of all available raw methods.
Warning |
---|
Raw methods embed strings directly into SQL query, SQL injection protection is required |
Code Block | ||||
---|---|---|---|---|
| ||||
builder::table('users')
->select_raw('id, name, :character as character', ["character" => "Good"])
->first();
//When working with builder only named params are allowed, however if you need to use other types, you might use (raw)sql class.
use core\dml\sql;
builder::table('users')
->select(new sql('id, name, ? as character, ["Good"])) // When you want to pass (raw)sql
->first();
// Raw functions work the same way as regular select and add_select,
// meaning that calling select_raw will override your select statement.
// Calling add_select_raw will append to your select statement,
// it will add a comma delimiter, you don't need to manage it manually
// You may also combine raw and non-raw statements
builder::table('users')
->select('id')
->add_select('name')
->add_select_raw(':character as character', ['character' => 'Good'])
->first(); |
id | name | character |
---|---|---|
1 | John Doe | Good |
Joins
Inner join
Query builder might join other tables for you. To perform an inner join (or just join for simplification) you may use the join method, the first argument is the name of the table to join, the remaining arguments define the column in the main table, the condition and the column in the table being joined.
Code Block | ||||
---|---|---|---|---|
| ||||
builder::table('orders')
->join('users', 'user_id', '=', 'id') // Don't be alarmed that there is naming collision, the columns will be auto-prefixed automatically
->select('*', 'users.name as user_name') // By default nothing from a joined table will be selected though, you need to append that manually
->first();
// Same result
builder::table('orders')
->join('users', 'user_id', 'id') // You can omit '=' condition in joins for convenience this will be equivalent to the code above
->select('*', 'users.name as user_name')
->first(); |
The above code will give you the following data selected:
id | user_id | goods_item_id | quantity | user_name |
---|---|---|---|---|
1 | 1 | 1 | 496 | John Doe |
Left join
To perform a left join use the following construct:
Code Block | ||||
---|---|---|---|---|
| ||||
builder::table('orders')
->left_join('users', 'user_id', '=', 'id')
->select('*', 'users.name as user_name')
->first(); |
Right join
Code Block | ||||
---|---|---|---|---|
| ||||
builder::table('orders')
->right_join('users', 'user_id', '=', 'id')
->select('*', 'users.name as user_name')
->first(); |
Full join
Code Block | ||||
---|---|---|---|---|
| ||||
builder::table('orders')
->full_join('users', 'user_id', '=', 'id')
->select('*', 'users.name as user_name')
->first(); |
Cross join
Cross join is slightly different from all the previous joins as it does not take any arguments, only the table name.
Code Block | ||||
---|---|---|---|---|
| ||||
builder::table('orders')
->cross_join('users')
->first(); |
Joining multiple tables
You may join multiple tables using the builder, simply call join several times on the builder object.
Code Block | ||||
---|---|---|---|---|
| ||||
builder::table('orders')
->join(['users', 'users'], 'user_id', 'id')
->join('goods', 'goods_item', 'id')
->select(['orders.id as order_id', 'users.name as user_name', 'goods.name as item_name', 'orders.quantity as quantity'])
->first(); |
Will give you the following result:
order_id | user_name | item_name | quantity |
---|---|---|---|
1 | John Doe | A brick | 496 |
Complex condition joins
Sometimes you want to join tables on multiple conditions, this also possible via the builder, you would need to use the following syntax:
Code Block | ||||
---|---|---|---|---|
| ||||
builder::table('orders')
->join('users', function(builder $joining, builder $builder) {
$joining->where_field('orders.id', 'users.id') // You can add as many where conditions as you want here
->where('users.name', '!=', 'Justin Bieber'); // Nah, no orders here
})
->first(); |
Alternative syntax
To define a custom alias for a joined table, you may use an alternative syntax for table field:
Code Block | ||||
---|---|---|---|---|
| ||||
use core\orm\query\table;
// Using table class
builder::table('orders')
->join((new table('users'))->as('u'), 'user_id', 'id')
->select('*', 'u.name as user_name')
->first();
// Or using array syntax
builder::table('orders')
->join(['users', 'u'], 'user_id', 'id')
->select('*', 'u.name as user_name')
->first();
// They both will achieve the same result
// If you examine the code, the second one internally will
// be translated to the first one, saving you from a cumbersome syntax constructs |
Sub query join
Sub query joins are possible, you will need to pass a query builder instance or subquery class instance instead of a table name to the join method. An alias must be specified for sub query joins.
Code Block | ||||
---|---|---|---|---|
| ||||
use core\orm\query\table;
use core\orm\query\subquery;
$sub_query = builder::table('users')
->where('status', 1);
// A basic way
builder::table('orders')
->join((new table($sub_query))->as('u'), 'user_id', 'id')
->select('*', 'u.name as user_name')
->first();
// Using short syntax
builder::table('orders')
->join([$sub_query, 'u'], 'user_id', 'id')
->select('*', 'u.name as user_name')
->first();
// Using subquery class
builder::table('orders')
->join((new subquery($sub_query))->as('u'), 'user_id', 'id')
->select('*', 'u.name as user_name')
->first(); |
Where clauses
Simple where clauses
You can use the where
method to add where conditions to your query. Where
accepts three arguments:
- Name of column
- The operator
[=, in, <, > , <> or !=, <=, >=, like, ilike, ...]
- The value to evaluate against the column
Where
provides a fluent interface and can be chained. By default the value will always end up as a named parameter, means it's protected against SQL injection.
Code Block | ||
---|---|---|
| ||
builder::table('users')
->where('status', '=', 1)
->fetch();
builder::table('users')
->where('votes', '<=' 50)
->where('status', '=', 1')
->fetch(); |
You can only supply two arguments, the column and the value. In this case depending on the value type, the operator defaults to =
or in
:
Section | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
OR statements
You can chain where clauses as well as add or_ clauses to the query. The or_where
function accepts the same arguments as the where
function.
Code Block | ||
---|---|---|
| ||
// WHERE name = 'John Doe' OR name = 'Jane Doe'
$users = builder::table('users')
->where('name', 'John Doe')
->or_where('name', 'Jane Doe')
->fetch(); |
Nested where clauses / parameter grouping
If you need to group where clauses you can use the nested where functionality. Just pass a Closure argument, which will receive the nested builder as an argument.
Code Block | ||
---|---|---|
| ||
// WHERE status = 1 AND (name = 'John Doe' OR name = 'Jane Doe')
$does = builder::table('users')
->where('status', 1)
->where(function (builder $builder) {
$builder->where('name', 'John Doe')
->or_where('name', 'Jane Doe')
})->fetch(); |
Additional where clauses
where_field / or_where_field
This function allows to compare two columns. Both fields are passed through a standard regex field name check here, that should prevent you from basic SQL injections.
Code Block | ||
---|---|---|
| ||
// WHERE last_login = created
$does = builder::table('users')
->where_field('last_login', 'created')
->fetch();
// WHERE last_login > created
$does = builder::table('users')
->where_field('last_login', '>', 'created')
->fetch(); |
where_null / or_where_null / where_not_null / or_where_not_null
This is a shortcut for ->
where('column', null);
Code Block | ||
---|---|---|
| ||
// WHERE last_login IS NULL
$does = builder::table('users')
->where_null('last_login')
->fetch();
// WHERE last_login IS NOT NULL
$does = builder::table('users')
->where_not_null('last_login')
->fetch(); |
where_in / or_where_in / where_not_in / or_where_not_in
This is a shortcut for ->
where('column', 'in', $value);
where_like / or_where_like / where_like_starts_with / or_where_like_starts_with / where_like_ends_with / or_where_like_ends_with
The builder provides some shortcuts for LIKE statements.
Section | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Section | ||||||||||||||||||
|
You can only supply two arguments, the column and the value. In this case depending on the value type, the operator defaults to =
or in
:
|
OR statements
...
|
By default the like comparisons are case-sensitive. If you need case-insensitive LIKE statements use: ilike, ilike_starts_with, ilike_ends_with
If you need NOT LIKE statements use: !like, !like_starts_with, !like_ends_with, !ilike, !ilike_starts_with, !ilike_ends_with
where_like_raw / or_where_like_raw
If none of the other like functions cover you need you can use the raw methods. The value won't be surrounded by any percentage signs, this needs to be handled by yourself.
Warning |
---|
If you use these function you'll insert a raw LIKE statement so it has a potential for introducing SQL injections. Also make sure you escape the values passed to it using $DB->sql_like_escape() . |
Code Block | ||
---|---|---|
| ||
// WHERE name = 'John Doe' OR name = 'Jane Doe' $users$does = builder::table('users') ->where('name', 'John Doe') ->or_where_like_raw('name', 'Jane DoeJohn%Doe') ->fetch(); |
Nested where clauses / Parameter grouping
...
|
where_exists / or_where_exists / where_not_exists / or_where_not_exists
The where_exists functions allows for creating a WHERE EXISTS (subquery) condition. It accepts a builder or a Closure argument, which will receive the nested builder as an argumenta new builder instance. This allows you to define the query that should be used inside the exists statement.
Code Block | ||
---|---|---|
| ||
// SELECT * // FROM orders // WHERE EXISTS (SELECT statusid =FROM 1users ANDWHERE (name =LIKE 'John Doe%Doe' ORAND nameid = 'Jane Doe'orders.user_id) $does$second_builder = builder::table('users') ->where>select('status', 1) ->where(function (builder $builder) { id') $builder->where('name', 'John like_ends_with', 'Doe') ->or>where_wherefield('nameusers.id', 'Jane Doe''orders.user_id'); $orders = builder::table('orders') ->where_exists($second_builder) })->fetch(); |
Additional where clauses
where_field / or_where_field
This function allows to compare two columns. Both fields are passed through a standard regex field name check here, that should prevent you from basic sql injections.
Code Block | ||
---|---|---|
| ||
// WHERE last_login =// createdAlternative $does$orders = builder::table('usersorders') ->where_field('last_login', 'created')exists(function(builder $builder) { $builder->fetch>select('id'); // WHERE last_login > created $does = builder::table('users') ->where_field('last_loginname', '>like_ends_with', 'created') ->fetch(); |
where_null / or_where_null / where_not_null / or_where_not_null
This is a shortcut for ->
where('column', null);
Code Block | ||
---|---|---|
| ||
// WHERE last_login IS NULL $does = builder::table('users')'Doe') ->where_nullfield('last_login'users.id', 'orders.user_id'); })->fetch(); |
Raw where clauses
If you need to pass a raw condition to your query you can either pass an instance of SQL to your clause:
Code Block | ||
---|---|---|
| ||
$sql = new sql("name // WHERE last_login IS NOT NULL $does= :name", ['name' => 'John']); $users = builder::table('users') ->where_not_null('last_login'($sql) ->fetch(); |
Alternative, use the provided raw methods, passing the SQL part as first and the params as second argument:
where_
...
raw
or_where_
...
This is a shortcut for ->
where('column', 'in', $value);
where_like / or_where_like / where_like_starts_with / or_where_like_starts_with / where_like_ends_with / or_where_like_ends_with
The builder provides some shortcuts for LIKE statements.
...
width | 50% |
---|
...
language | php |
---|
...
raw
Code Block | ||
---|---|---|
| ||
$users = builder::table('users')
->where_raw("name = :name", ['name' => 'John'])
->fetch();
|
Warning |
---|
Raw methods embed strings directly into SQL query, SQL injection protection is required. |
Complex example
Section | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
By default the like comparisons are case-sensitive. If you need case-insensitive LIKE statements use: ilike, ilike_starts_with, ilike_ends_with
If you need NOT LIKE statements use: !like, !like_starts_with, !like_ends_with, !ilike, !ilike_starts_with, !ilike_ends_with
where_like_raw / or_where_like_raw
If none of the other like functions cover you need you can use the raw methods. The value won't be surrounded by any percentage signs, this needs to be handled by yourself.
Warning | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Please note: If you use these function you'll insert a raw LIKE statement so it has a potential for introducing SQL injections. Also make sure you escape the values passed to it using $DB->sql_like_escape()
|
Order
Order by a column
It's quite easy to add order statements to the query using query builder. To simply order by a column name you should use order_by method.
It takes the column name to sort by as first argument, the second argument is optional defining order direction, ascending (asc) or descending (desc), defaulting to ascending.
Code Block | ||
---|---|---|
| ||
builder::table('orders')
->select(['id', 'name'])
->order_by('name', 'asc')
->limit(2)
->get();
|
This will give you the following result
id | name |
---|---|
2 | Jane Doe |
1 | John Doe |
Order by multiple columns
If you need to order by multiple columns subsequent calls to order_by will achieve that. Each call to order_by appends to order condition SQL.
Code Block | ||
---|---|---|
| ||
$does = builder::table('usersorders') ->select(['id', 'name', 'status']) ->order_by('status', 'asc') ->where>order_like_rawby('name', 'John%Doeasc') ->fetch ->limit(2) ->get(); |
where_exists / or_where_exists / where_not_exists / or_where_not_exists
...
This would give you the following results:
id | name | status |
---|---|---|
7 | Stephen Monroe | 0 |
4 | Adam Welsh | 1 |
Builders also allows you to reset any order condition added, by calling reset_order_by()
method or passing null
to the order_by()
method.
Code Block | ||
---|---|---|
| ||
// SELECT * // FROM orders // WHERE EXISTS (SELECT id FROM users WHERE name LIKE '%Doe' AND id = orders.user_id) $second_builder = builder::table('usersorders') ->select(['id', 'name']) ->where ->order_by('namestatus', 'like_ends_with'asc') ->order_by('name', 'Doeasc') ->where_field('users. ->reset_order_by() ->order_by('id', 'orders.user_iddesc'); $orders = builder::table('orders') ->where_exists($second_builder) ->fetch(); // Alternative $orders = builder::table('orders') ->where_exists(function(builder $builder) { $builder->select('id') ->where('name', 'like_ends_with', 'Doe') ->where_field('users.id', 'orders.user_id'); })->fetch(); |
Raw where clauses
If you need to pass a raw condition to your query you can either pass an instance of sql to your clause:
Code Block | ||
---|---|---|
| ||
$sql = new sql("name = :name", ['name' => 'John']);
$users = builder::table('users')
->where($sql)
->fetch();
|
or you use the provided raw methods, passing the sql part as first and the params as second argument:
where_raw
or_where_raw
Code Block | ||
---|---|---|
| ||
$users = builder::table('users')
->where_raw("name = :name", ['name' => 'John'])
->fetch();
|
Warning |
---|
Raw methods embed strings directly into sql query, sql injection protection is required! |
Complex example
Section | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Order
Order by a column
It's quite easy to add order statements to the query using query builder. To simply order by a column name you should use order_by method.
It takes the column name to sort by as first argument, the second argument is optional defining order direction, ascending (asc) or descending (desc), defaulting to ascending.
This will give you the following result
...
Order by multiple columns
If you need to order by multiple columns subsequent calls to order_by will achieve that. Each call to order_by appends to order condition sql.
This would give you the following results
...
Builders also allows you to reset any order condition added, by calling reset_order_by()
method or passing null
to the order_by()
method.
The above code would give you the following result:
...
Info | ||
---|---|---|
| ||
Currently it behaves slightly differently from select methods naming wise. Calling select overrides current select sql and calling add_select() appends to it, order_by() doesn't have add_order_by() and appends to the order condition to override it, however it allows you to call do_not_order() to reset order list. |
Order by raw statement
Similar to column names passed to select and add_select methods, column names are validated to protect you against accidental sql-injection. However if you require order by raw statement it is achievable calling order_by_raw method
Warning |
---|
Raw methods embed strings directly into sql query, sql injection protection is required! |
You may also use (raw) sql with order_by
Grouping
Group by a column
If you need to group results by a column, builder allows you to do that and there is a group_by method to do that.
Should give you the following result
...
Group by multiple columns
If you need to group results by multiple columns, builder allows you to do that doing multiple calls to group_by method.
Builders also allows you to reset any order condition added, by calling reset_group_by()
method or passing null
to the group_by()
method.
Group by raw value
If you need some more complex grouping, you may resort to a raw method group_by_raw to embed a raw string into the query. It works in a similar manner to order_by_raw and add_select_raw
Warning |
---|
Raw methods embed strings directly into sql query, sql injection protection is required! |
Offsetting or limiting the results
limit()
Will add limit to the results
Code Block | ||
---|---|---|
| ||
$users = builder::table('users')
->limit(1)
->get();
// Usually used together with limit
$users = builder::table('users')
->first(); // Calling first will add limit 1 to the query |
offset()
Will add offset to the query
Code Block | ||
---|---|---|
| ||
$users = builder::table('users')
->offset(1)
->get();
// Usually used together with limit
$users = builder::table('users')
->offset(1)
->limit(1)
->get(); |
Warning |
---|
Due to the DB driver limitations limits can not be used within any form of subqueries. If you add a limit there, you will get a debugging notice |
Unions
The query builder also provides easy means to unite 2 queries together. Here is how it works.
This what the result of $users
would be:
...
Field auto-prefixing
Short version:
If you do not specify an alias on your builder then the builder automatically will use the table name (without the prefix) as the alias. You can specify your own alias by using the as()
function or pass it as second argument to the builder::table()
function.
All columns used in select, add_select, group_by, having, or_having, where, or_where, join
, etc. will automatically be prefixed with the proper alias. You can also manually specify the alias in the column with the dot-syntax (i.e. 'users.name
').
Detailed version:
It might seem that when using query builder and referring to fields the field prefix is not included. That's on the surface only to make it easier for developers. All the fields passed to the fielder without using raw functions which includes: select, add_select, group_by, having, or_having, where, or_where, join, etc. prefix fields with either a table name given (without moodle table prefix, e.g. mdl_). On the query builder you may specify an alias for the table the builder is for by calling as() or by passing the alias as second argument to the builder::table()
function. This will result in all fields passed to the builder to be prefixed with the given alias when sql is generated. If however the alias is not set, the fields still will be prefixed but with the table name instead, in the form of "{table}". For joined tables, they will be automatically aliased with the table name without moodle prefix e.g. for mdl_user it will pre prefixed with user. This behaviour is designed to allow more natural flow when building query, for example you can add where('user.name', 'John) instead of where('{user}.name', 'John). It's smart enough not to prefix fields if they are already prefixed, or add prefix correctly if as or aggregate function is specified. How it works behind the scenes, if string is passed to the method that accepts a field, it wraps it in a field() class which in turn is responsible to add prefix if needed. Field class accepts a name and a link to a builder instance it should belong to. All these functions support overloading and you may pass a field class directly, if you didn't link it to any other builder, it will be linked to the current automatically. Field class has some built in subclasses for convenience, including raw_field and subquery (technically field is a subclass of raw field). Subquery accepts a builder and allows you to specify an alias as. The application is to use it in select, where you need to select a subquery.
...
->limit(2)
->get();
|
The above code would give you the following result:
id | name |
---|---|
2 | Jane Doe |
1 | John Doe |
Info |
---|
Currently it behaves slightly differently from select methods naming wise. Calling select overrides current select SQL and calling add_select() appends to it, order_by() doesn't have add_order_by() and appends to the order condition to override it, however it allows you to call do_not_order() to reset order list. |
Order by raw statement
Similar to column names passed to select and add_select methods, column names are validated to protect you against accidental SQL injection. However if you require order by raw statement it is achievable calling order_by_raw method:
Code Block | ||
---|---|---|
| ||
builder::table('orders')
->select(['id', 'name'])
->order_by_raw('id asc, name desc')
->limit(2)
->get();
|
Warning |
---|
Raw methods embed strings directly into SQL query, SQL injection protection is required. |
You may also use (raw) sql with order_by:
Code Block | ||
---|---|---|
| ||
use core\dml\sql;
builder::table('orders')
->select(['id', 'name'])
->order_by(new sql('name asc'))
->limit(2)
->get();
|
Grouping
Group by a column
If you need to group results by a column, builder allows you to do that and there is a group_by method to do that.
Code Block | ||
---|---|---|
| ||
builder::table('orders')
->join('users', 'id', 'user_id')
->select(['users.name as user_name', 'max(quantity) as max_quantity'])
->group_by('users.name')
->order_by('max(quantity)')
->limit(2)
->get();
|
Should give you the following result:
user_name | max_quantity |
---|---|
John Doe | 496 |
Ivan Svobodin | 5 |
Group by multiple columns
If you need to group results by multiple columns, builder allows you to do that doing multiple calls to group_by method.
Code Block | ||
---|---|---|
| ||
builder::table('orders')
->join('users', 'id', 'user_id')
->select(['users.name as user_name', 'max(quantity) as max_quantity', 'orders.id'])
->group_by('users.name')
->group_by('id')
->order_by('max(quantity)')
->limit(2)
->get();
|
Builders also allows you to reset any order condition added, by calling reset_group_by()
method or passing null
to the group_by()
method.
Group by raw value
If you need some more complex grouping, you may resort to a raw method group_by_raw to embed a raw string into the query. It works in a similar manner to order_by_raw and add_select_raw:
Code Block | ||
---|---|---|
| ||
builder::table('orders')
->join('users', 'id', 'user_id')
->select(['users.name as user_name', 'max(quantity) as max_quantity'])
->group_by_raw('"users".name')
->order_by('max(quantity)')
->limit(2)
->get();
|
Warning |
---|
Raw methods embed strings directly into SQL query, SQL injection protection is required. |
Offsetting or limiting the results
limit()
Will add limit to the results:
Code Block | ||
---|---|---|
| ||
$users = builder::table('users')
->limit(1)
->get();
// Usually used together with limit
$users = builder::table('users')
->first(); // Calling first will add limit 1 to the query |
offset()
Will add offset to the query:
Code Block | ||
---|---|---|
| ||
$users = builder::table('users')
->offset(1)
->get();
// Usually used together with limit
$users = builder::table('users')
->offset(1)
->limit(1)
->get(); |
Warning |
---|
Due to the DB driver limitations limits can not be used within any form of subqueries. If you add a limit there, you will get a debugging notice. |
Unions
The query builder also provides easy means to unite two queries together. Here is how it works.
Code Block | ||
---|---|---|
| ||
$gmail_users = builder::table('users')
->select(['id', 'name'])
->where('email', 'like_ends_with', 'gmail.com');
$users = builder::table('users')
->select_raw('id, name')
->where('email', 'like_ends_with', 'gmail.com')
->union($gmail_users)
->get();
// You may also do union all, however that might result in debugging notice
// due to the fact that $DB always requires the first column to be unique :shrug:
$all = builder::table('users')
->select_raw('id, name')
->union_all($gmail_users)
->get();
|
This what the result of $users
would be:
id | name |
---|---|
1 | John Doe |
2 | Jane Doe |
6 | Alisson Carr |
Field auto-prefixing
If you do not specify an alias on your builder then the builder automatically will use the table name (without the prefix) as the alias. You can specify your own alias by using the as()
function or pass it as second argument to the builder::table()
function.
All columns used in select, add_select, group_by, having, or_having, where, or_where, join
, etc. will automatically be prefixed with the proper alias. You can also manually specify the alias in the column with the dot-syntax (i.e. 'users.name
').
In more detail
It might seem that when using query builder and referring to fields the field prefix is not included. That's on the surface only to make it easier for developers. All the fields passed to the fielder without using raw functions which includes: select, add_select, group_by, having, or_having, where, or_where, join, etc. prefix fields with either a table name given (without moodle table prefix, e.g. mdl_). On the query builder you may specify an alias for the table the builder is for by calling as() or by passing the alias as second argument to the builder::table()
function. This will result in all fields passed to the builder to be prefixed with the given alias when SQL is generated. If however the alias is not set, the fields still will be prefixed but with the table name instead, in the form of "{table}".
For joined tables, they will be automatically aliased with the table name without moodle prefix e.g. for mdl_user it will pre prefixed with user. This behaviour is designed to allow more natural flow when building query, for example you can add where('user.name', 'John) instead of where('{user}.name', 'John). It's smart enough not to prefix fields if they are already prefixed, or add prefix correctly if as or aggregate function is specified. How it works behind the scenes, if string is passed to the method that accepts a field, it wraps it in a field() class which in turn is responsible to add prefix if needed. Field class accepts a name and a link to a builder instance it should belong to.
All these functions support overloading and you may pass a field class directly, if you didn't link it to any other builder, it will be linked to the current automatically. Field class has some built in subclasses for convenience, including raw_field and subquery (technically field is a subclass of raw field). Subquery accepts a builder and allows you to specify an alias as. The application is to use it in select, where you need to select a subquery.
Let's get to some code examples to get better understanding of all this.
Code Block | ||
---|---|---|
| ||
// Will generate the following sql before it goes to the DML layer:
// SELECT "users".id, "users".name FROM {users} users WHERE 1 = 1
builder::table('users')
->select(['id', 'name'])
->get();
// Will generate the following sql before it goes to the DML layer:
// SELECT "u".id, "u".name FROM {users} u WHERE 1 = 1
builder::table('users')
->as('u')
->select(['id', 'name'])
->get();
// Will generate the following sql before it goes to the DML layer:
// SELECT "u".id, "u".name, "orders".quantity
// FROM {users} u
// JOIN {orders} orders ON ("u.id" = "orders".user_id)
// WHERE 1 = 1
// ORDER BY "u".name ASC
builder::table('users', 'u')
->join('orders', 'id', 'user_id')
->select(['id', 'name', 'orders.quantity'])
->order_by('name')
->get();
|
Raw methods
In addition to some query building methods query builder offers a raw version which has a suffix '_raw' and accepts 2 two arguments: sql SQL and parameters. These methods embed provided sql in the appropriate part of the query.
List of available raw methods:
...
Warning |
---|
Raw methods embed strings directly into sql SQL query, sql SQL injection protection is required!. |
Mapping of results
By default all the methods which retrieve results (find, find_or_fail, fetch, get, fetch_recordset, paginate, first, one
) will return an instance of stdClass
for a single record.
However, you can influence that behaviour and either have records returned as arrays
or as any custom objects
.
Map to array()
Just call results_as_arrays()
anytime before calling one of the methods mentioned above.
Code Block | ||
---|---|---|
| ||
$users = builder::table('users') ->results_as_arrays() ->fetch(); |
Map to stdClass
This is the default behaviour which will make sure each record is represented by a stdClass
instance.
Code Block | ||
---|---|---|
| ||
$users = builder::table('users') ->results_as_objects() ->fetch(); |
Map to custom objects
Use the map_to()
function to map to your own objects. The map_to
function either accepts a Closure or a class name, which it will create instances of when retrieving the results. It will pass the result from the database as an argument to the Closure or as into the constructor of the given class.
...
You can combine results_as_arrays()
and map_to()
if you want your custom class accept the record as an array
rather than a stdClass
instance.
Conditional Clauses
When \ Unless
Sometimes you want to conditionally include clauses in your query.
...
Code Block | ||
---|---|---|
| ||
$builder = builder::table('users'); if ($only_active) { $builder->where('status', 1); } $users = $builder->fetch(); |
you You can use the when() / unless()
functions. They accept a condition as first parameter and a closure as second parameter. The Closure will receive the current builder as argument.
...
Code Block | ||
---|---|---|
| ||
// if ($only_active) { ... } else { ... } $users = builder::table('users') ->when($only_active, function (builder $builder) { $builder->where('status', 1); }, function (builder $builder) { $builder->where('status', 0); })->fetch(); |
Tap
Tap is a shortcut for Sometimes an unconditional when() (->when(true, callable)
) can be helpful. You can use this to group your builder statements, or if you are building groups of statements and then joining them together.
Code Block | ||
---|---|---|
| ||
// It might be useful for debugging purposes as well as if you have one fluent flow // and you need a builder reference without braking the flow. $users = builder::table('users') ->where('status', 1) ->where('votes', '>', 50) ->tap>when(true, function (builder $builder) { var_dump($builder); }) ->where('name', 'like', 'John') ->fetch(); |
Modifying data
The builder can not only be used to retrieve data but also to modify data.
Insert
Use the insert()
method to insert new rows into the table. No further conditions or query parts can be used.
Section | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Update
You can update multiple rows in a table by using the update()
method. Please note that update does not accepts an array/object containing an idID. If you want to update a single record you can either use the where condition or use update_record().
You can only use where()
conditions for update().
Section | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Warning | |
---|---|
Oops | Due to database support limitation, you can use only where part of the query for update statements, if you try to add any joins, you will get an exception, when attempting to execute the query. |
Update Record
You can update a single record in two ways, first using →update() in conjunction with a proper where condition or ->update_record().
Section | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Delete
You can delete multiple records in table by using the delete()
method.
...
Section | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Warning | title | Oops
---|
Due to database support limitation, you can use only where part of the query for delete statements, if you try to add any joins, you will get an exception, when attempting to execute the query. |
Differences with Laravel's
...
query builder
Missing where methods
At this stage we don't have the following methods for some reason or another:
- where_between / or_where_between and where_not_between / or_where_not_between haven: Haven't been implemented yet, don't see any reason why not, they should just translate to 2 two wheres internally
- whereDate / whereMonth / whereDay / whereYear / whereTime havenwhereTime: Haven't been implemented, they require some more investigation, due to the fact that they use internal database functions, might be doable with some additions to dml DML layer.
- where_column / or_where_column originally column: Originally implemented as (or_)where_field, we might revisit it, p.s. generally what Laravel refers to as column we refer to as field in quite a lot of places
- JSON where clauses - require : Require support at the dml DML level, by the time of writing I believe all database engines we use have built-in json support with a slightly different syntax.
Missing features
- Change locking type, query : Query builder provides a way to use shared locks \ locks for update, this implementation leaves that out to dml.DML
- Increment \ Decrement 2 methods : Two methods that take field (column) name as argument and increment or decrement a given field respectively according to a given where clause
- update_or_insert - not insert: Not implemented yet, it takes 2 two arrays as arguments, it uses first to query a record from database and then if not found it inserts it with attributes from both arrays or either if found, it updates the record with attributes from a second array.
- in_random_order: - not Not implemented, requires support on a dml DML level, as sql SQL varies slightly from dbms to dbms. To simplify what it does, it adds ORDER BY RANDOM()
Behavioural differences
- In laravel Laravel select and add_select functions take multiple arguments to add multiple columns to select, in the current implementation the functions take one argument either as string/field object or an array of strings / field objects to select multiple fields in one statement.
- In the current implementation field prefixing is more advanced
See more about Laravel's Query Builderquery builder on their website.