TableLevelQueryTrait
Table of Contents
Methods
- asJson() : bool|string
- asObject() : mixed
- columns() : static
- This defines the table column names to return from the CDatabase
- createOrUpdate() : object|array<string|int, mixed>
- Acronym for saveOrUpdate
- 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 CDatabase based on the pkField given
- get() : object|array<string|int, mixed>|null
- Fetches a single item from the CDatabase.
- getOrThrow() : object|array<string|int, mixed>|null
- Get a resource or throw an exception if it does not exist
- has() : bool
- This checks if the table has a record that matches the where clause
- 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 CDatabase based on the pkField clause
- random() : array<string|int, mixed>|mixed|object
- Fetches random n items from the table, default to 1
- raw() : Raw
- save() : object
- Saves and returns the saved item as an object
- saveAll() : array<string|int, mixed>|PDOStatement|null
- Save multiple items in the database.
- saveOrUpdate() : object|array<string|int, mixed>
- Create a new item or update an existing item Supports both single and multiple items
- update() : PDOStatement|null
- checkFilterMode() : void
- This prevents the use of non-filtering methods in filter mode.
Methods
asJson()
public
asJson() : bool|string
Tags
Return values
bool|stringasObject()
public
asObject() : mixed
Tags
columns()
This defines the table column names to return from the CDatabase
public
columns([string|array<string|int, mixed> $columns = "*" ]) : static
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
staticcreateOrUpdate()
Acronym for saveOrUpdate
public
createOrUpdate(array<string|int, mixed>|Arrayable $data[, string $pkField = 'id' ]) : object|array<string|int, mixed>
Parameters
- $data : array<string|int, mixed>|Arrayable
- $pkField : string = 'id'
Tags
Return values
object|array<string|int, mixed>deleteAll()
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 CDatabase 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>|nullget()
Fetches a single item from the CDatabase.
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>|nullgetOrThrow()
Get a resource or throw an exception if it does not exist
public
getOrThrow([int|array<string|int, mixed>|string|null $where = null ][, string $message = 'Item not found' ][, string|null $idField = 'id' ]) : object|array<string|int, mixed>|null
Parameters
- $where : int|array<string|int, mixed>|string|null = null
- $message : string = 'Item not found'
- $idField : string|null = 'id'
Tags
Return values
object|array<string|int, mixed>|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
booljoin()
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 CDatabase 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>|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
Rawsave()
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
saveAll()
Save multiple items in the database.
public
saveAll(array<string|int, mixed> $data[, bool $returning = true ]) : array<string|int, mixed>|PDOStatement|null
Parameters
- $data : array<string|int, mixed>
-
The data to save. Must be an associative array
- $returning : bool = true
-
If true, it returns the resultset containing the saved items
Tags
Return values
array<string|int, mixed>|PDOStatement|nullsaveOrUpdate()
Create a new item or update an existing item Supports both single and multiple items
public
saveOrUpdate(array<string|int, mixed>|Arrayable $data[, string $pkField = 'id' ]) : object|array<string|int, mixed>
Parameters
- $data : array<string|int, mixed>|Arrayable
- $pkField : string = 'id'
Tags
Return values
object|array<string|int, mixed>update()
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|nullcheckFilterMode()
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);