Leaf PHP

Db extends Core
in package

Leaf Db ----- Simple database interactions

Tags
version
3.0
since

v2.1.0

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
self

addConnections()

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
Core

all()

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
Core

backup()

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
self

bind()

Bind parameters to a query

public bind(mixed ...$bindings) : self
Parameters
$bindings : mixed
Return values
self

close()

Closes Db connection

public close() : void

column()

Fetch column from results

public column() : mixed

commit()

Commit the current transaction

public commit() : self
Return values
self

config()

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
Core

connection()

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
PDO

count()

Get the current count of objects in query

public count() : int
Return values
int

create()

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
self

createTable()

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
self

drop()

Drop a database if it exists

public drop(string $db) : self
Parameters
$db : string

The name of the database to drop

Return values
self

dropTable()

Drop a database table

public dropTable(string $table) : mixed
Parameters
$table : string
Tags
params

string $table The name of the table to drop

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
author

Milos Lukic https://github.com/iammiloslukic

hidden()

Hide particular fields from the final value returned

public hidden(mixed ...$values) : self
Parameters
$values : mixed

The value(s) to hide

Return values
self

insert()

Add a new row in a db table

public insert(string $table) : self
Parameters
$table : string

Db Table

Return values
self

last()

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
Core

obj()

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
self

orWhereJson()

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
self

orWhereJsonContains()

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
self

params()

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
self

query()

Manually create a database query

public query(string $sql) : self
Parameters
$sql : string

Full db query

Return values
self

result()

Get raw result of last query

public result() : PDOStatement
Return values
PDOStatement

rollback()

Rollback the current transaction

public rollback() : self
Return values
self

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>|null

select()

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
self

tableExists()

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
self

use()

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
Core

where()

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
self

whereJson()

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
self

whereJsonContains()

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
self

with()

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
self

clearState()

Prepare leaf db to handle next query

protected clearState() : mixed

copyState()

Copy internal state

protected copyState() : mixed

dsn()

protected dsn() : string
Return values
string

env()

Returns the value of the environment variable by using Leaf's `_env` primarily.

private env(string $name) : string|bool
Parameters
$name : string
Return values
string|bool

        
On this page

Search results