Database
- Full name:
\Qubus\Expressive\Database - Parent interfaces:
\Qubus\Expressive\Singleton,\Qubus\Expressive\Table,\Qubus\Expressive\Select,\Qubus\Expressive\Where,\Qubus\Expressive\Insert,\Qubus\Expressive\Update,\Qubus\Expressive\Set,\Qubus\Expressive\Delete,\Qubus\Expressive\Join,\Qubus\Expressive\Aggregate
Methods
schema
The associated schema instance.
transactional
Run transactional queries.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$callback | \Closure | transaction callback |
$that | mixed | |
$default | mixed |
Throws:
Inherited methods
count
Return the aggregate count of column
Parameters:
| Parameter | Type | Description |
|---|---|---|
$column | string|null | - the column name |
max
Return the aggregate max count of column
Parameters:
| Parameter | Type | Description |
|---|---|---|
$column | string | - the column name |
min
Return the aggregate min count of column
Parameters:
| Parameter | Type | Description |
|---|---|---|
$column | string | - the column name |
sum
Return the aggregate sum count of column
Parameters:
| Parameter | Type | Description |
|---|---|---|
$column | string | - the column name |
avg
Return the aggregate average count of column
Parameters:
| Parameter | Type | Description |
|---|---|---|
$column | string | - the column name |
aggregate
Parameters:
| Parameter | Type | Description |
|---|---|---|
$fn | string | - The function to use for the aggregation |
join
Build a join
public join(string $tableName, string $constraint, string $tableAlias = '', string $joinOperator = \self::JOIN_LEFT): \Qubus\Expressive\Database
Parameters:
| Parameter | Type | Description |
|---|---|---|
$tableName | string | |
$constraint | string | -> id = profile.user_id |
$tableAlias | string | - The alias of the table name |
$joinOperator | string | - LEFT | INNER | etc... |
on
An alias to join by using a Database instance.
public on(\Qubus\Expressive\Database $query, string $joinOperator = \self::JOIN_LEFT): \Qubus\Expressive\Database
The Database instance may have select and where statement for the ON clause
Parameters:
| Parameter | Type | Description |
|---|---|---|
$query | \Qubus\Expressive\Database | |
$joinOperator | string |
getJoinOnString
Create the JOIN ... ON string when there is a join. It will be called by on().
delete
Delete rows.
Use the query builder to create the where clause.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$deleteAll | bool | When there is no where condition, setting to true will delete all. |
set
To set data for update or insert $key can be an array for mass set
Parameters:
| Parameter | Type | Description |
|---|---|---|
$key | mixed | |
$value | mixed|null |
save
Save, a shortcut to update() or insert().
update
Update entries.
Use the query builder to create the where clause.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$data | array|null | the data to update |
returning
Returning (Postgres etc.)
Parameters:
| Parameter | Type | Description |
|---|---|---|
$cols | string |
upsert
Upsert (basic support)
Parameters:
| Parameter | Type | Description |
|---|---|---|
$conflictCols | array | |
$updateData | array |
lastInsertId
Retrieves the ID of the last record inserted.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$pk | string|null |
insert
Insert new rows $data can be 2-dimensional to add a bulk insert If a single row is inserted, it will return its row instance
Parameters:
| Parameter | Type | Description |
|---|---|---|
$data | array | - data to populate |
where
Add where condition, more calls appends with AND.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$condition | mixed | condition possibly containing ? or :name |
$parameters | mixed | array accepted by PDOStatement::execute or a scalar value. |
wherePK
Where Primary key
Parameters:
| Parameter | Type | Description |
|---|---|---|
$id | int|string |
whereNot
WHERE $columName != $value
Parameters:
| Parameter | Type | Description |
|---|---|---|
$columnName | string | |
$value | mixed |
whereLike
WHERE $columName LIKE $value
Parameters:
| Parameter | Type | Description |
|---|---|---|
$columnName | string | |
$value | mixed |
whereNotLike
WHERE $columName NOT LIKE $value
Parameters:
| Parameter | Type | Description |
|---|---|---|
$columnName | string | |
$value | mixed |
whereGt
WHERE $columName > $value
Parameters:
| Parameter | Type | Description |
|---|---|---|
$columnName | string | |
$value | mixed |
whereGte
WHERE $columName >= $value
Parameters:
| Parameter | Type | Description |
|---|---|---|
$columnName | string | |
$value | mixed |
whereLt
WHERE $columName < $value
Parameters:
| Parameter | Type | Description |
|---|---|---|
$columnName | string | |
$value | mixed |
whereLte
WHERE $columName <= $value
Parameters:
| Parameter | Type | Description |
|---|---|---|
$columnName | string | |
$value | mixed |
whereIn
WHERE $columName IN (?,?,?,...)
Parameters:
| Parameter | Type | Description |
|---|---|---|
$columnName | string | |
$values | array |
whereNotIn
WHERE $columName NOT IN (?,?,?,...)
Parameters:
| Parameter | Type | Description |
|---|---|---|
$columnName | string | |
$values | array |
whereNull
WHERE $columName IS NULL
Parameters:
| Parameter | Type | Description |
|---|---|---|
$columnName | string |
whereNotNull
WHERE $columName IS NOT NULL
Parameters:
| Parameter | Type | Description |
|---|---|---|
$columnName | string |
orderBy
ORDER BY $columnName (ASC | DESC)
Parameters:
| Parameter | Type | Description |
|---|---|---|
$columnName | string | - The name of the colum or an expression |
$ordering | string | (DESC | ASC) |
limit
LIMIT $limit
Parameters:
| Parameter | Type | Description |
|---|---|---|
$limit | int|null |
offset
OFFSET $offset
Parameters:
| Parameter | Type | Description |
|---|---|---|
$offset | int|null |
pagination
Parameters:
| Parameter | Type | Description |
|---|---|---|
$perPage | int | |
$page | int |
and
Create an AND operator in the where clause
or
Create an OR operator in the where clause
wrap
To group multiple where clauses together.
query
To execute a raw query
public query(string $query, array $parameters = [], bool $returnAsPdoStmt = false): \Qubus\Expressive\Database|\PDOStatement
Parameters:
| Parameter | Type | Description |
|---|---|---|
$query | string | |
$parameters | array | |
$returnAsPdoStmt | bool | True, it will return the PDOStatement |
| false, it will return $this, which can be used for chaining | ||
| or access the properties of the results. |
find
To find all rows and create their instances Use the query builder to build the where clause or $this->query with select If a callback function is provided, the 1st arg must accept the rows results
public find(callable|null $callback = null): callable|bool|int|\SplFixedArray|string|\ArrayIterator|\InternalIterator|array|\Iterator
$this->find(function($rows){ // do more stuff here... });
Parameters:
| Parameter | Type | Description |
|---|---|---|
$callback | callable|null | Run a function on the returned rows |
findOne
Return one row
Parameters:
| Parameter | Type | Description |
|---|---|---|
$id | int|string|null | Use to fetch by primary key. |
select
Create the select clause.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$columns | mixed | The column(s) to select. Can be string or array of fields. |
$alias | string|null | An alias to the column. |
getSelectFields
Return the select fields as array.
fromArray
Create an instance from the given row (an associative array of data fetched from the database).
Parameters:
| Parameter | Type | Description |
|---|---|---|
$data | array |
having
Parameters:
| Parameter | Type | Description |
|---|---|---|
$statement | mixed | |
$operator | string |
groupBy
GROUP BY $columnName
Parameters:
| Parameter | Type | Description |
|---|---|---|
$columnName | string |
table
Define the working table and create a new instance
Parameters:
| Parameter | Type | Description |
|---|---|---|
$tableName | string | Table name. |
$alias | ?string | The table alias name. |
getTableName
Return the name of the table.
setTableAlias
Set the table alias.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$alias | string |
getTableAlias
Get table Alias
setStructure
public setStructure(string $primaryKeyName = 'id', string $foreignKeyName = '%s_id'): \Qubus\Expressive\Database
Parameters:
| Parameter | Type | Description |
|---|---|---|
$primaryKeyName | string | The primary key, ie: id |
$foreignKeyName | string | The foreign key as a pattern: %s_id, |
| where %s will be substituted with the table name |
setTablePrefix
Parameters:
| Parameter | Type | Description |
|---|---|---|
$tablePrefix | string|null |
getTablePrefix
Return the table prefix.
getStructure
Return the table structure.
getPrimaryKeyname
Get the primary key name.
getForeignKeyname
Get foreign key name.
fromInstance
public static fromInstance(\Qubus\Expressive\Connection $connection, string $primaryKeyName = 'id', string|null $tablePrefix = null): \Qubus\Expressive\Database
- This method is static. Parameters:
| Parameter | Type | Description |
|---|---|---|
$connection | \Qubus\Expressive\Connection | |
$primaryKeyName | string | |
$tablePrefix | string|null |