VELOX API Docs

Database extends PDO
in package

A class that represents the database and handles database operations.

Example:

$database = Database::instance();
$database->query('SELECT * FROM `users`');
$database->prepare('SELECT * FROM `users` WHERE `job` = :job LIMIT 5')->execute([':job' => 'Developer'])->fetchAll();
$database->perform('SELECT * FROM `users` WHERE `title` LIKE :title AND `id` > :id', ['title' => 'Dr.%', 'id' => 1])->fetchAll();
Tags
since
1.3.0

Table of Contents

$cache  : array<string|int, mixed>
A cache to hold prepared statements.
$connections  : array<string|int, mixed>
Current open database connections.
$dsn  : string
$options  : array<string|int, mixed>|null
$password  : string|null
$username  : string|null
connect()  : static
Returns a singleton instance of the `Database` class based on connection credentials.
instance()  : static
Returns the singleton instance of the `Database` class using credentials found in `{database}` config.
perform()  : PDOStatement
A wrapper method to perform a query on the fly using either `self::query()` or `self::prepare()` + `self::execute()`.
prepare()  : mixed
Adds caching capabilities for prepared statement.
transactional()  : mixed
Serves as a wrapper method to execute some operations in transactional context with the ability to attempt retires.
__construct()  : mixed
Class constructor.
getStatementClass()  : string
Returns FQN for a custom `PDOStatement` class.
mock()  : Database
Returns a fake instance of the `Database` class.

Properties

$cache

A cache to hold prepared statements.

protected array<string|int, mixed> $cache

$connections

Current open database connections.

protected static array<string|int, mixed> $connections

$options

protected array<string|int, mixed>|null $options

$password

protected string|null $password

$username

protected string|null $username

Methods

connect()

Returns a singleton instance of the `Database` class based on connection credentials.

public final static connect(string|null $dsn[, string|null $username = null ][, string|null $password = null ][, array<string|int, mixed>|null $options = null ]) : static

This method makes sure that a single connection is opened and reused for each connection credentials set (DSN, User, Password, ...).

Parameters
$dsn : string|null

The DSN string.

$username : string|null = null

[optional] The database username.

$password : string|null = null

[optional] The database password.

$options : array<string|int, mixed>|null = null

[optional] PDO options.

Return values
static

instance()

Returns the singleton instance of the `Database` class using credentials found in `{database}` config.

public static instance() : static
Tags
codeCoverageIgnore

This method is overridden (mocked) in tests.

Return values
static

perform()

A wrapper method to perform a query on the fly using either `self::query()` or `self::prepare()` + `self::execute()`.

public perform(string $query[, array<string|int, mixed> $params = null ]) : PDOStatement
Parameters
$query : string

The query to execute.

$params : array<string|int, mixed> = null

The parameters to bind to the query.

Return values
PDOStatement

prepare()

Adds caching capabilities for prepared statement.

public prepare(mixed $query[, mixed $options = [] ]) : mixed
Parameters
$query : mixed
$options : mixed = []
Return values
mixed

transactional()

Serves as a wrapper method to execute some operations in transactional context with the ability to attempt retires.

public transactional(callable $callback[, int $retries = 3 ]) : mixed
Parameters
$callback : callable

The callback to execute inside the transaction. This callback will be bound to the Database class.

$retries : int = 3

The number of times to attempt the transaction. Each retry will be delayed by 1-3 seconds.

Tags
throws
RuntimeException

If the transaction fails after all retries.

Return values
mixed

The result of the callback.

__construct()

Class constructor.

protected __construct(string $dsn[, string|null $username = null ][, string|null $password = null ][, array<string|int, mixed>|null $options = null ]) : mixed

Adds some default options to the PDO connection.

Parameters
$dsn : string
$username : string|null = null
$password : string|null = null
$options : array<string|int, mixed>|null = null
Return values
mixed

getStatementClass()

Returns FQN for a custom `PDOStatement` class.

private getStatementClass() : string
Return values
string

mock()

Returns a fake instance of the `Database` class.

private static mock() : Database
Tags
codeCoverageIgnore
Return values
Database

This instance will throw an exception if a method is called.

Search results