Db
extends Core
in package
Leaf Db ----- Simple database interactions
Tags
Table of Contents
Properties
- $added : mixed
- Items to add to query results
- $bindings : mixed
- Params bound to query
- $config : mixed
- Config for leaf db
- $connections : mixed
- List of connected db instances
- $currentConnection : string|null
- Current connection to use for db
- $eager : mixed
- Items to eager load
- $errors : mixed
- Errors caught in leaf db
- $hidden : mixed
- Items to hide from query results
- $params : mixed
- Full list of params passed into leaf db
- $query : mixed
- Actual query to run
- $queryResult : PDOStatement
- Query result
- $table : mixed
- Db table to peform operations on
- $uniques : mixed
- Items which should be unique in db
Methods
- __construct() : mixed
- Initialize leaf db with a database connection
- add() : self
- Add particular fields to the final value returned
- addConnections() : Core
- Add a list of database connections
- all() : mixed
- Alias for fetchAll
- assoc() : mixed
- Alias for fetchAssoc
- autoConnect() : Core
- Connect to database using environment variables
- backup() : mixed
- Backup a database
- beginTransaction() : self
- Begin a database transaction
- bind() : self
- Bind parameters to a query
- close() : void
- Closes Db connection
- column() : mixed
- Fetch column from results
- commit() : self
- Commit the current transaction
- config() : mixed
- Configure leaf db - syncs with leaf config
- connect() : Core
- Connect to database
- connection() : mixed
- Return the database connection
- connectSync() : PDO
- Connect to database immediately
- count() : int
- Get the current count of objects in query
- create() : self
- Create a database if it doesn't exist
- createTable() : mixed
- Create database table
- createTableIfNotExists() : mixed
- Create database table
- debug() : mixed
- Get the current snapshot of leaf db internals
- delete() : self
- Delete a table's records
- drop() : self
- Drop a database if it exists
- dropTable() : mixed
- Drop a database table
- errors() : array<string|int, mixed>
- Return caught errors if any
- execute() : mixed
- Execute a generated query
- fetchAll() : mixed
- Fetch the items returned by query
- fetchAssoc() : mixed
- Fetch the items returned by query
- fetchObj() : mixed
- Fetch the items returned by query
- find() : mixed
- Add a find by id clause to query
- first() : mixed
- Find the first matching item for current query
- get() : mixed
- Alias for fetchAll
- groupBy() : mixed
- Group query results by a column
- hidden() : self
- Hide particular fields from the final value returned
- insert() : self
- Add a new row in a db table
- last() : mixed
- Find the last matching item for current query
- lastInsertId() : mixed
- Returns the ID of the last inserted row or sequence value
- limit() : mixed
- Limit query items by a specific number
- load() : Core
- Alias for connect
- obj() : mixed
- Alias for fetchObj
- offset() : mixed
- Offset query items by a specific number
- orderBy() : mixed
- Order query items by a specific
- orWhere() : self
- Add a where clause with OR comparator to db query
- orWhereJson() : self
- Add a JSON where clause with OR comparator to the query
- orWhereJsonContains() : self
- Add a JSON contains clause with OR comparator to the query
- params() : self
- Pass in parameters into your query
- query() : self
- Manually create a database query
- result() : PDOStatement
- Get raw result of last query
- rollback() : self
- Rollback the current transaction
- search() : array<string|int, mixed>|null
- Search a db table for a value
- select() : mixed
- Retrieve a row from table
- table() : self
- Set the current db table for operations
- tableExists() : bool
- Check if a database table exists
- transaction() : bool
- Run a database transaction
- unique() : mixed
- Make sure a value doesn't already exist in a table to avoid duplicates.
- update() : self
- Update a row in a db table
- use() : Core
- Set the current connection to use for queries
- where() : self
- Add a where clause to db query
- whereJson() : self
- Add a JSON where clause to the query
- whereJsonContains() : self
- Add a JSON contains clause to the query
- with() : self
- Fetch current query with all related data
- clearState() : mixed
- Prepare leaf db to handle next query
- copyState() : mixed
- Copy internal state
- dsn() : string
- env() : string|bool
- Returns the value of the environment variable by using Leaf's `_env` primarily.
Properties
$added
Items to add to query results
protected
mixed
$added
= []
$bindings
Params bound to query
protected
mixed
$bindings
= []
$config
Config for leaf db
protected
mixed
$config
= ['dbtype' => 'mysql', 'charset' => null, 'port' => '3306', 'unixSocket' => null, 'host' => '127.0.0.1', 'username' => 'root', 'password' => '', 'dbname' => '']
$connections
List of connected db instances
protected
mixed
$connections
= []
$currentConnection
Current connection to use for db
protected
string|null
$currentConnection
= null
$eager
Items to eager load
protected
mixed
$eager
= []
$errors
Errors caught in leaf db
protected
mixed
$errors
= []
$hidden
Items to hide from query results
protected
mixed
$hidden
= []
$params
Full list of params passed into leaf db
protected
mixed
$params
= []
$query
Actual query to run
protected
mixed
$query
$queryResult
Query result
protected
PDOStatement
$queryResult
$table
Db table to peform operations on
protected
mixed
$table
= null
$uniques
Items which should be unique in db
protected
mixed
$uniques
= []
Methods
__construct()
Initialize leaf db with a database connection
public
__construct([string|array<string|int, mixed> $host = '' ][, string $dbname = '' ][, string $user = 'root' ][, string $password = '' ][, string $dbtype = 'mysql' ]) : mixed
Parameters
- $host : string|array<string|int, mixed> = ''
-
Host Name or full config
- $dbname : string = ''
-
Database name
- $user : string = 'root'
-
Database username
- $password : string = ''
-
Database password
- $dbtype : string = 'mysql'
-
Type of database: mysql, postgres, sqlite, ...
add()
Add particular fields to the final value returned
public
add(string|array<string|int, mixed> $name[, string $value = null ]) : self
Parameters
- $name : string|array<string|int, mixed>
-
What to add
- $value : string = null
-
The value to add
Return values
selfaddConnections()
Add a list of database connections
public
addConnections(array<string|int, mixed> $connections[, string|null $default = null ]) : Core
Parameters
- $connections : array<string|int, mixed>
-
List of database connections
- $default : string|null = null
Return values
Coreall()
Alias for fetchAll
public
all([mixed $type = 'assoc' ]) : mixed
Parameters
- $type : mixed = 'assoc'
assoc()
Alias for fetchAssoc
public
assoc() : mixed
autoConnect()
Connect to database using environment variables
public
autoConnect([array<string|int, mixed> $pdoOptions = [] ]) : Core
Parameters
- $pdoOptions : array<string|int, mixed> = []
-
Options for PDO connection
Return values
Corebackup()
Backup a database
public
backup(string $dbName, string $destination[, string $withDifferential = false ]) : mixed
Parameters
- $dbName : string
-
The name of the database to backup
- $destination : string
-
The path to backup database to
- $withDifferential : string = false
-
Whether to use differential backups or not
beginTransaction()
Begin a database transaction
public
beginTransaction() : self
Return values
selfbind()
Bind parameters to a query
public
bind(mixed ...$bindings) : self
Parameters
- $bindings : mixed
Return values
selfclose()
Closes Db connection
public
close() : void
column()
Fetch column from results
public
column() : mixed
commit()
Commit the current transaction
public
commit() : self
Return values
selfconfig()
Configure leaf db - syncs with leaf config
public
config(mixed $name[, mixed $value = null ]) : mixed
Parameters
- $name : mixed
- $value : mixed = null
connect()
Connect to database
public
connect([string|array<string|int, mixed> $host = '127.0.0.1' ][, string $dbname = '' ][, string $user = 'root' ][, string $password = '' ][, string $dbtype = 'mysql' ][, array<string|int, mixed> $pdoOptions = [] ]) : Core
Parameters
- $host : string|array<string|int, mixed> = '127.0.0.1'
-
Host Name or full config
- $dbname : string = ''
-
Database name
- $user : string = 'root'
-
Database username
- $password : string = ''
-
Database password
- $dbtype : string = 'mysql'
-
Type of database: mysql, postgres, sqlite, ...
- $pdoOptions : array<string|int, mixed> = []
-
Options for PDO connection
Return values
Coreconnection()
Return the database connection
public
connection([PDO|string|null $connection = null ]) : mixed
Parameters
- $connection : PDO|string|null = null
-
Manual instance of PDO connection
connectSync()
Connect to database immediately
public
connectSync([string|array<string|int, mixed> $host = '127.0.0.1' ][, string $dbname = '' ][, string $user = 'root' ][, string $password = '' ][, string $dbtype = 'mysql' ][, array<string|int, mixed> $pdoOptions = [] ]) : PDO
Parameters
- $host : string|array<string|int, mixed> = '127.0.0.1'
-
Host Name or full config
- $dbname : string = ''
-
Database name
- $user : string = 'root'
-
Database username
- $password : string = ''
-
Database password
- $dbtype : string = 'mysql'
-
Type of database: mysql, postgres, sqlite, ...
- $pdoOptions : array<string|int, mixed> = []
-
Options for PDO connection
Return values
PDOcount()
Get the current count of objects in query
public
count() : int
Return values
intcreate()
Create a database if it doesn't exist
public
create(string $db) : self
Parameters
- $db : string
-
The name of the database to create
Return values
selfcreateTable()
Create database table
public
createTable(string $table[, array<string|int, mixed> $fields = [] ]) : mixed
Parameters
- $table : string
-
The name of the database table to create
- $fields : array<string|int, mixed> = []
-
The fields to create
createTableIfNotExists()
Create database table
public
createTableIfNotExists(string $table[, array<string|int, mixed> $fields = [] ]) : mixed
Parameters
- $table : string
-
The name of the database table to create
- $fields : array<string|int, mixed> = []
-
The fields to create
debug()
Get the current snapshot of leaf db internals
public
debug() : mixed
delete()
Delete a table's records
public
delete(string $table) : self
Parameters
- $table : string
-
: Db Table
Return values
selfdrop()
Drop a database if it exists
public
drop(string $db) : self
Parameters
- $db : string
-
The name of the database to drop
Return values
selfdropTable()
Drop a database table
public
dropTable(string $table) : mixed
Parameters
- $table : string
Tags
errors()
Return caught errors if any
public
errors() : array<string|int, mixed>
Return values
array<string|int, mixed>execute()
Execute a generated query
public
execute() : mixed
fetchAll()
Fetch the items returned by query
public
fetchAll([mixed $type = 'assoc' ]) : mixed
Parameters
- $type : mixed = 'assoc'
fetchAssoc()
Fetch the items returned by query
public
fetchAssoc() : mixed
fetchObj()
Fetch the items returned by query
public
fetchObj() : mixed
find()
Add a find by id clause to query
public
find(string|int $id) : mixed
Parameters
- $id : string|int
-
The id of the row to find
first()
Find the first matching item for current query
public
first() : mixed
get()
Alias for fetchAll
public
get([mixed $type = 'assoc' ]) : mixed
Parameters
- $type : mixed = 'assoc'
groupBy()
Group query results by a column
public
groupBy(string $column) : mixed
Parameters
- $column : string
-
The column to group results by
Tags
hidden()
Hide particular fields from the final value returned
public
hidden(mixed ...$values) : self
Parameters
- $values : mixed
-
The value(s) to hide
Return values
selfinsert()
Add a new row in a db table
public
insert(string $table) : self
Parameters
- $table : string
-
Db Table
Return values
selflast()
Find the last matching item for current query
public
last() : mixed
lastInsertId()
Returns the ID of the last inserted row or sequence value
public
lastInsertId([string|null $name = null ]) : mixed
Parameters
- $name : string|null = null
-
Name of the sequence object from which the ID should be returned.
limit()
Limit query items by a specific number
public
limit(string|number $limit) : mixed
Parameters
- $limit : string|number
-
The number to limit by
load()
Alias for connect
public
load([string|array<string|int, mixed> $host = '127.0.0.1' ][, string $dbname = '' ][, string $user = 'root' ][, string $password = '' ][, string $dbtype = 'mysql' ][, array<string|int, mixed> $pdoOptions = [] ]) : Core
Parameters
- $host : string|array<string|int, mixed> = '127.0.0.1'
-
Host Name or full config
- $dbname : string = ''
-
Database name
- $user : string = 'root'
-
Database username
- $password : string = ''
-
Database password
- $dbtype : string = 'mysql'
-
Type of database: mysql, postgres, sqlite, ...
- $pdoOptions : array<string|int, mixed> = []
-
Options for PDO connection
Return values
Coreobj()
Alias for fetchObj
public
obj() : mixed
offset()
Offset query items by a specific number
public
offset(string|number $offset) : mixed
Parameters
- $offset : string|number
-
The number to offset by
orderBy()
Order query items by a specific
public
orderBy(string $column[, string $direction = 'desc' ]) : mixed
Parameters
- $column : string
-
The column to order results by
- $direction : string = 'desc'
-
The direction to order [DESC, ASC]
orWhere()
Add a where clause with OR comparator to db query
public
orWhere(string|array<string|int, mixed> $condition[, mixed $comparator = null ][, mixed $value = null ]) : self
Parameters
- $condition : string|array<string|int, mixed>
-
The condition to evaluate
- $comparator : mixed = null
-
Condition value or comparator
- $value : mixed = null
-
The value of condition if comparator is passed
Return values
selforWhereJson()
Add a JSON where clause with OR comparator to the query
public
orWhereJson(string $column, string $jsonKey, mixed $value[, string $comparator = '=' ]) : self
Parameters
- $column : string
-
The JSON column
- $jsonKey : string
-
The key within the JSON structure
- $value : mixed
-
The value to compare against
- $comparator : string = '='
-
The comparison operator (default '=')
Return values
selforWhereJsonContains()
Add a JSON contains clause with OR comparator to the query
public
orWhereJsonContains(string $column, mixed $value[, string|null $jsonKey = null ]) : self
Parameters
- $column : string
-
The JSON column
- $value : mixed
-
The value to check for
- $jsonKey : string|null = null
-
The key within the JSON structure (optional)
Return values
selfparams()
Pass in parameters into your query
public
params(array<string|int, mixed>|string $params) : self
Parameters
- $params : array<string|int, mixed>|string
-
Key or params to pass into query
Return values
selfquery()
Manually create a database query
public
query(string $sql) : self
Parameters
- $sql : string
-
Full db query
Return values
selfresult()
Get raw result of last query
public
result() : PDOStatement
Return values
PDOStatementrollback()
Rollback the current transaction
public
rollback() : self
Return values
selfsearch()
Search a db table for a value
public
search(string $row, string $value[, array<string|int, mixed>|null $hidden = [] ]) : array<string|int, mixed>|null
Parameters
- $row : string
-
The item to search for in table
- $value : string
-
The keyword to search for
- $hidden : array<string|int, mixed>|null = []
-
The items to hide from returned result
Return values
array<string|int, mixed>|nullselect()
Retrieve a row from table
public
select(string $table[, string $items = '*' ]) : mixed
Parameters
- $table : string
-
Db Table
- $items : string = '*'
-
Specific table columns to fetch
table()
Set the current db table for operations
public
table(string $table) : self
Parameters
- $table : string
-
Table to perform database operations on
Return values
selftableExists()
Check if a database table exists
public
tableExists(string $table) : bool
Parameters
- $table : string
Return values
bool —true if the table exists
transaction()
Run a database transaction
public
transaction(callable $callback) : bool
Parameters
- $callback : callable
-
The callback to run
Return values
bool —Whether the transaction completed successfully
unique()
Make sure a value doesn't already exist in a table to avoid duplicates.
public
unique(mixed ...$uniques) : mixed
Parameters
- $uniques : mixed
-
Items to check for
update()
Update a row in a db table
public
update(string $table) : self
Parameters
- $table : string
-
Db Table
Return values
selfuse()
Set the current connection to use for queries
public
use([string|null $connection = null ]) : Core
Parameters
- $connection : string|null = null
-
The name of the connection to use
Return values
Corewhere()
Add a where clause to db query
public
where(string|array<string|int, mixed> $condition[, mixed $comparator = null ][, mixed $value = null ]) : self
Parameters
- $condition : string|array<string|int, mixed>
-
The condition to evaluate
- $comparator : mixed = null
-
Condition value or comparator
- $value : mixed = null
-
The value of condition if comparator is passed
Return values
selfwhereJson()
Add a JSON where clause to the query
public
whereJson(string $column, string $jsonKey, mixed $value[, string $comparator = '=' ]) : self
Parameters
- $column : string
-
The JSON column
- $jsonKey : string
-
The key within the JSON structure
- $value : mixed
-
The value to compare against
- $comparator : string = '='
-
The comparison operator (default '=')
Return values
selfwhereJsonContains()
Add a JSON contains clause to the query
public
whereJsonContains(string $column, mixed $value[, string|null $jsonKey = null ]) : self
Parameters
- $column : string
-
The JSON column
- $value : mixed
-
The value to check for
- $jsonKey : string|null = null
-
The key within the JSON structure (optional)
Return values
selfwith()
Fetch current query with all related data
public
with(string $table[, string $foreignKey = null ]) : self
Parameters
- $table : string
-
The table to join
- $foreignKey : string = null
-
The foreign key to use
Return values
selfclearState()
Prepare leaf db to handle next query
protected
clearState() : mixed
copyState()
Copy internal state
protected
copyState() : mixed
dsn()
protected
dsn() : string
Return values
stringenv()
Returns the value of the environment variable by using Leaf's `_env` primarily.
private
env(string $name) : string|bool
Parameters
- $name : string