| lang | php |
|---|
Model Query Builder class is providing a bridge between Model instance and the database by transforming PHP code into SQL queries.
In order to use methods of this class, you need to have a Model instance. You can get a Model instance by using ee('Model') service and getting or making a new model.
$existingTemplate = ee('Model')->get('Template');
$newTemplate = ee('Model')->make('Template');
class ExpressionEngine\Service\Model\Query\Builder
[TOC=3]
Run the query limited to one result and return the first element in the Collection
| Parameter | Type | Description |
|---|---|---|
| $cache | Bool |
Whether the query results should be cached. Defaults to false |
| Returns | Mixed |
The first element in the collection (or NULL if empty) |
Run the query and return the results as a Collection
| Parameter | Type | Description |
|---|---|---|
| $cache | Bool |
Whether the query results should be cached. Defaults to false |
| Returns | Mixed |
All elements in the collection (or NULL if empty) |
Updates the model(s) data by running SQL UPDATE query.
NOTE: It's not recommended to use this method directly. Use save() method of the model instead.
| Parameter | Type | Description |
|---|---|---|
| Returns | Void |
Inserts new database record for model by running SQL INSERT query.
NOTE: It's not recommended to use this method directly. Use save() method of the model instead.
| Parameter | Type | Description |
|---|---|---|
| Returns | Void |
Delete the model(s) from database by running SQL DELETE query.
| Parameter | Type | Description |
|---|---|---|
| Returns | Void |
Get the number of records that match criterias by running SQL COUNT query
| Parameter | Type | Description |
|---|---|---|
| Returns | Int |
The number of matching models |
Performs search using LIKE operator on specified columns.
Subsequent calls to this method will be chained with AND operator.
| Parameter | Type | Description |
|---|---|---|
| $properties | `String | Array` |
| $value | String |
String value to search |
| Returns | Builder |
Modified instance of Query Builder |
Filter the query by adding a WHERE clause to the SQL query.
Note: when third parameter is omited, second parameter is used as value and == operator is used.
Subsequent calls to this method will be chained with AND operator.
| Parameter | Type | Description |
|---|---|---|
| $property | String |
Model property name |
| $operator | String |
Comparison operator. (List of supported operators)[development/services/model/fetching.md#available-filters]. Defaults to == |
| $value | String |
Value to compare to |
| Returns | Builder |
Modified instance of Query Builder |
Same as filter() but chained with OR operator.
Used to group filters with AND operator. Should be closed with endFilterGroup() method.
Used to group filters with OR operator. Should be closed with endFilterGroup() method.
Indicates the end of filter group.
Limits the SELECT part of SQL query to only fetch certain model properties.
Sets the model property with SQL SET so it could be saved to database later
| Parameter | Type | Description |
|---|---|---|
| $property | String |
Model property name |
| $value | String |
New property value |
| Returns | Builder |
Modified instance of Query Builder |
Adds related model to the query by performing SQL JOIN
| Parameter | Type | Description |
|---|---|---|
| $property | `String | Array` |
| Returns | Builder |
Modified instance of Query Builder |
Name of property to order by, converted to SQL ORDER BY clause
| Parameter | Type | Description |
|---|---|---|
| Returns | Builder |
Modified instance of Query Builder |
Limit the query by applying SQL LIMIT clause
| Parameter | Type | Description |
|---|---|---|
| $limit | Int |
Number to limit by. Defaults to 2^64 |
| Returns | Builder |
Modified instance of Query Builder |
Limit the query by applying offset to SQL LIMIT clause
| Parameter | Type | Description |
|---|---|---|
| $offset | Int |
Number to offset. Defaults to 0 |
| Returns | Builder |
Modified instance of Query Builder |