BaseBuilder
in package
Uses
TableLevelQueryTrait, AggregateTrait
Provides a basis for other query builders to base on.
Table of Contents
Constants
- PORM_VERSION = "1.0.8"
- The version of the Porm package
Properties
- $database : Database
- $database : mixed
- The Database object to use
- $alias : string|null
- $allowFilterOnly : bool
- $columns : string|array<string|int, mixed>|null
- $preventHas : bool
- $preventRaw : true
- $resultSet : mixed
- $table : mixed
- The Database table to use. This is for interoperability with other versions of Porm
- $where : array<string|int, mixed>
Methods
- __construct() : mixed
- asJson() : bool|string
- asObject() : mixed
- avg() : string|null
- columns() : BaseBuilder
- This defines the table column names to return from the Database
- count() : int|null
- deleteAll() : PDOStatement|null
- This deletes all items that match the where clause
- deleteById() : PDOStatement|null
- This is under the hood similar to deleteOne but it is more explicit
- filter() : Builder
- This switches the query to filter mode. It is useful for conditional querying.
- first() : object|array<string|int, mixed>|null
- This grabs the first [n] items from the Database based on the pkField given
- from() : BaseBuilder
- This sets the table to use
- fromContainer() : BaseBuilder|null
- This grabs the database connection from the container if we are using a container
- get() : object|array<string|int, mixed>|null
- Fetches a single item from the Database.
- getDatabase() : Database|null
- has() : bool
- This checks if the table has a record that matches the where clause
- info() : array<string|int, mixed>
- Returns the details of the current db connection
- inTransaction() : void
- Using transactions. This is a wrapper for the action method in the Core class.
- join() : object|null
- Opens the portal to the joins builder. Once you call this, you can call the join methods
- last() : object|array<string|int, mixed>|null
- Grab the last item from the Database based on the pkField clause
- logLastQuery() : string|null
- Logs the last query that was run
- logs() : array<string|int, mixed>
- Return all executed queries.
- max() : string|null
- min() : string|null
- random() : array<string|int, mixed>|mixed|object
- Fetches random n items from the table, default to 1
- raw() : Raw
- rawQuery() : mixed
- This assists to perform raw sql queries
- save() : object
- Saves and returns the saved item as an object
- sum() : string|null
- table() : BaseBuilder
- This is for running queries. Should be called first
- update() : PDOStatement|null
- using() : TableLevelQueryTrait
- This sets the connection to the Database to use for the current query.
- setup() : void
- Sets up the database connection
- checkFilterMode() : void
- This prevents the use of non-filtering methods in filter mode.
Constants
PORM_VERSION
The version of the Porm package
public
string
PORM_VERSION
= "1.0.8"
Properties
$database
public
Database
$database
The Database object to use
$database
The Database object to use
public
mixed
$database
$alias
private
string|null
$alias
The alias to use, will defualt to the table name provided.
$allowFilterOnly
private
bool
$allowFilterOnly
= false
Lock out the use of any other method other than filter
$columns
private
string|array<string|int, mixed>|null
$columns
= '*'
The columns to select
$preventHas
private
bool
$preventHas
= false
Lock out the use of filter
$preventRaw
private
true
$preventRaw
= false
Lock out the use of raw queries
$resultSet
private
mixed
$resultSet
The result set to call asObject and asJson on.
$table
The Database table to use. This is for interoperability with other versions of Porm
private
mixed
$table
$where
private
array<string|int, mixed>
$where
= []
Methods
__construct()
public
__construct([ContainerInterface|Database|string|null $connection = 'db' ][, string|null $containDbKey = 'database' ]) : mixed
Parameters
- $connection : ContainerInterface|Database|string|null = 'db'
- $containDbKey : string|null = 'database'
asJson()
public
asJson() : bool|string
Tags
Return values
bool|stringasObject()
public
asObject() : mixed
Tags
avg()
public
avg(string $column, array<string|int, mixed>|null $where) : string|null
Parameters
- $column : string
- $where : array<string|int, mixed>|null
Tags
Return values
string|nullcolumns()
This defines the table column names to return from the Database
public
columns([string|array<string|int, mixed> $columns = "*" ]) : BaseBuilder
If you're in join mode, then all ambigous columns should define the table as an alias
Parameters
- $columns : string|array<string|int, mixed> = "*"
-
The columns to select defaults to * for all.
Tags
Return values
BaseBuilder —The current Porm object
count()
public
count([string|null $column = null ][, array<string|int, mixed>|null $where = null ]) : int|null
Parameters
- $column : string|null = null
- $where : array<string|int, mixed>|null = null
Tags
Return values
int|nulldeleteAll()
This deletes all items that match the where clause
public
deleteAll(array<string|int, mixed> $where) : PDOStatement|null
Parameters
- $where : array<string|int, mixed>
Tags
Return values
PDOStatement|nulldeleteById()
This is under the hood similar to deleteOne but it is more explicit
public
deleteById(string|int $id[, string|null $idField = 'id' ]) : PDOStatement|null
Parameters
- $id : string|int
- $idField : string|null = 'id'
Tags
Return values
PDOStatement|nullfilter()
This switches the query to filter mode. It is useful for conditional querying.
public
filter([array<string|int, mixed>|null $where = [] ]) : Builder
Parameters
- $where : array<string|int, mixed>|null = []
-
The where clause to use
Tags
Return values
Builderfirst()
This grabs the first [n] items from the Database based on the pkField given
public
first([int|null $size = 1 ][, array<string|int, mixed>|null $where = [] ][, string $pkField = 'id' ]) : object|array<string|int, mixed>|null
Parameters
- $size : int|null = 1
-
The number of items to fetch
- $where : array<string|int, mixed>|null = []
-
The where clause to use
- $pkField : string = 'id'
-
The primary key field to use
Tags
Return values
object|array<string|int, mixed>|nullfrom()
This sets the table to use
public
static from(string $table[, string|null $alias = null ][, string|null $using = null ]) : BaseBuilder
Parameters
- $table : string
-
The table to use
- $alias : string|null = null
-
The alias to use
- $using : string|null = null
-
The connection to use
Tags
Return values
BaseBuilderfromContainer()
This grabs the database connection from the container if we are using a container
public
fromContainer(ContainerInterface $container[, string|null $containDbKey = 'database' ]) : BaseBuilder|null
Parameters
- $container : ContainerInterface
- $containDbKey : string|null = 'database'
Tags
Return values
BaseBuilder|nullget()
Fetches a single item from the Database.
public
get([int|array<string|int, mixed>|string|null $where = null ][, string|null $idField = 'id' ]) : object|array<string|int, mixed>|null
If the where clause is not passed, it fetches the last item in the table. If the where clause is an integer, it fetches the item with the id. If the where clause is an array, it fetches the item that matches the where clause. If the where clause is null, it fetches the last item in the table.
Parameters
- $where : int|array<string|int, mixed>|string|null = null
- $idField : string|null = 'id'
-
defaults to id, pass this if you want to use a different field as the id other than id
Tags
Return values
object|array<string|int, mixed>|nullgetDatabase()
public
getDatabase() : Database|null
Return values
Database|nullhas()
This checks if the table has a record that matches the where clause
public
has(string|array<string|int, mixed>|int|null $where[, string|null $pkField = 'id' ]) : bool
Parameters
- $where : string|array<string|int, mixed>|int|null
- $pkField : string|null = 'id'
Tags
Return values
boolinfo()
Returns the details of the current db connection
public
info() : array<string|int, mixed>
Return values
array<string|int, mixed>inTransaction()
Using transactions. This is a wrapper for the action method in the Core class.
public
inTransaction(callable $callback) : void
To access data outside the transaction, Create a result variable and refer to the transaction callback with the keyword use, and you can get data back after when you assign it from inside.
Parameters
- $callback : callable
-
The callback to run. It should return a void.
Tags
join()
Opens the portal to the joins builder. Once you call this, you can call the join methods
public
join([array<string|int, mixed>|null $where = null ]) : object|null
Parameters
- $where : array<string|int, mixed>|null = null
Tags
Return values
object|nulllast()
Grab the last item from the Database based on the pkField clause
public
last([int|null $size = 1 ][, array<string|int, mixed>|null $where = [] ][, string $pkField = 'id' ]) : object|array<string|int, mixed>|null
Parameters
- $size : int|null = 1
-
The number of items to fetch
- $where : array<string|int, mixed>|null = []
-
The where clause to use
- $pkField : string = 'id'
-
The primary key field to use
Tags
Return values
object|array<string|int, mixed>|nulllogLastQuery()
Logs the last query that was run
public
logLastQuery() : string|null
Return values
string|null —The last query that was run
logs()
Return all executed queries.
public
logs() : array<string|int, mixed>
This function will only return all records if set logging => true on initialization, otherwise it will only return one last record by default.
Return values
array<string|int, mixed> —The columns to select
max()
public
max(string $column, array<string|int, mixed>|null $where) : string|null
Parameters
- $column : string
- $where : array<string|int, mixed>|null
Tags
Return values
string|nullmin()
public
min(string $column, array<string|int, mixed>|null $where) : string|null
Parameters
- $column : string
- $where : array<string|int, mixed>|null
Tags
Return values
string|nullrandom()
Fetches random n items from the table, default to 1
public
random([int|null $limit = 1 ][, array<string|int, mixed>|null $where = null ]) : array<string|int, mixed>|mixed|object
Parameters
- $limit : int|null = 1
- $where : array<string|int, mixed>|null = null
Tags
Return values
array<string|int, mixed>|mixed|objectraw()
public
raw(string $query[, array<string|int, mixed>|null $params = [] ]) : Raw
Parameters
- $query : string
-
The query to run
- $params : array<string|int, mixed>|null = []
-
The parameters to pass prepare along with the query
Tags
Return values
RawrawQuery()
This assists to perform raw sql queries
public
static rawQuery(string $query[, array<string|int, mixed>|null $params = [] ][, string|null $using = 'db' ]) : mixed
Parameters
- $query : string
- $params : array<string|int, mixed>|null = []
- $using : string|null = 'db'
Tags
save()
Saves and returns the saved item as an object
public
save(array<string|int, mixed> $data) : object
Parameters
- $data : array<string|int, mixed>
-
The data to save. Must be an associative array
Tags
Return values
object —The saved object
sum()
public
sum(string $column, array<string|int, mixed>|null $where) : string|null
Parameters
- $column : string
- $where : array<string|int, mixed>|null
Tags
Return values
string|nulltable()
This is for running queries. Should be called first
public
static table(string $table[, string|null $alias = null ][, string|null $using = null ]) : BaseBuilder
Parameters
- $table : string
- $alias : string|null = null
- $using : string|null = null
Tags
Return values
BaseBuilderupdate()
public
update(array<string|int, mixed> $data, array<string|int, mixed>|int|string $where[, string|null $idField = 'id' ]) : PDOStatement|null
Parameters
- $data : array<string|int, mixed>
- $where : array<string|int, mixed>|int|string
- $idField : string|null = 'id'
Tags
Return values
PDOStatement|nullusing()
This sets the connection to the Database to use for the current query.
public
using([string|Database|BaseBuilder|ContainerInterface $connection = 'db' ][, string|null $containerDbKey = null ]) : TableLevelQueryTrait
It can be used to switch between Database connections.
Parameters
- $connection : string|Database|BaseBuilder|ContainerInterface = 'db'
-
The connection to use, defaults to 'db'
- $containerDbKey : string|null = null
Tags
Return values
TableLevelQueryTraitsetup()
Sets up the database connection
protected
setup(mixed $connection) : void
Parameters
- $connection : mixed
checkFilterMode()
This prevents the use of non-filtering methods in filter mode.
private
checkFilterMode([string $msg = 'Query is in filter mode, you cannot use this method in filter mode' ]) : void
Case here is like calling get() on join() yet join() return no resultset yet.
Parameters
- $msg : string = 'Query is in filter mode, you cannot use this method in filter mode'
-
The message to throw
This is primarily used internally for the purpose.
$this->checkFilterMode("You cannot delete at this point in the query, check the usage of the `delete()` method in the query builder for ".$this->table);