Documentation

PdbConfig extends Collection
in package

The configuration for a Pdb object.

This provides per-connection settings.

Table of Contents

Constants

HACK_MYSQL_TZ_NO_SESSION  : mixed = 'mysql_tz_no_session'
Don't set the session TZ, for when the MySQL has no TZ data.
HACK_MYSQL_TZ_OFFSET  : mixed = 'mysql_tz_offset'
If MySQL hasn't got TZ data installed, convert any IANA zone names to an offset.
HACK_NO_ENGINE_SUBSTITUTION  : mixed = 'no_engine_substitution'
HACK_SQLITE_FUNCTIONS  : mixed = 'sqlite_functions'
Insert some custom functions.
HACK_TIME_ZONE  : mixed = 'time_zone'
TX_ENABLE_NESTED  : mixed = 0x4
Enable nested transaction (via savepoints).
TX_FORCE_COMMIT_KEYS  : mixed = 0x8
Force the user to provide a transaction key when calling `commit()`.
TX_STRICT_COMMIT  : mixed = 0x1
Require an active transaction when calling `commit()`.
TX_STRICT_ROLLBACK  : mixed = 0x2
Require an active transaction when calling `rollback()`.
TYPE_MSSQL  : mixed = 'mssql'
TYPE_MYSQL  : mixed = 'mysql'
TYPE_ORACLE  : mixed = 'oracle'
TYPE_PGSQL  : mixed = 'pgsql'
TYPE_SQLITE  : mixed = 'sqlite'

Properties

$_pdo  : PDO|null
This is used in the Sprout compatibility layer.
$attributes  : array<string|int, mixed>
Connection attributes.
$cache  : PdbCache|string|array<string|int, mixed>
A caching class extending PdbCache.
$character_set  : string
Connection charset, default utf8.
$collation  : string
Default collation, default utf8.
$database  : string
Database name.
$dsn  : string|null
Connection string override.
$formatters  : array<string|int, callable|PdbDataFormatterInterface|string>
String formatters for class parameters.
$hacks  : array<string|int, string>
Driver specific hacks.
$host  : string
Connection hostname or IP address.
$identity  : string|null
An identity key for this connection.
$inflector  : InflectorInterface|string|array<string|int, mixed>
An inflector config for pluralisation and such.
$namespace  : string
Namespace for UUIDv5 generation.
$pass  : string
Connection password.
$port  : int|null
Port number, or null for default.
$prefix  : string
Global table prefix.
$schema  : string
Database schema, for Postgres.
$session  : array<string|int, mixed>
Session variables per call to `connect()`.
$socket  : string|bool
Treat the host as a unix socket for MySQL connections.
$sslmode  : string
Secure mode, for Postgres.
$table_prefixes  : array<string, string>
Per-table prefixes.
$timeout  : int
Connection timeout.
$timezone  : string|null
Set the timezone for this connection.
$transaction_mode  : int
TX enum
$ttl  : int
Default caching TTL, in seconds.
$type  : string
One of the TYPE enum.
$use_system_timezone  : bool
Use the PHP timezone for `Pdb::now()`.
$user  : string
Connection username.
$_checksums  : DirtyChecksums|null

Methods

__construct()  : mixed
__serialize()  : array<string|int, mixed>
__unserialize()  : void
extraFields()  : array<string|int, callable>
Define extra fields that you _might_ want included in the `toArray()` output.
fields()  : array<string|int, callable|bool>
These are fields for use in `toArray()`.
getChecksums()  : DirtyChecksums
Get a checksums store for this object.
getDsn()  : string
The 'Data Source Name' used to connect to the database.
getFieldQuotes()  : array<string|int, string>
Get parameter quotes as appropriate for the underlying DBMS.
getHack()  : mixed
Is this hack enabled?
getHacks()  : array<string|int, mixed>
The hacks applied to this configuration.
getIdentity()  : string
The identity key for this config.
getIterator()  : Traversable
getPrefixes()  : array<string|int, string>
Get prefixing rules.
jsonSerialize()  : mixed
offsetExists()  : bool
offsetGet()  : mixed
offsetSet()  : void
offsetUnset()  : void
toArray()  : array<string|int, mixed>
Convert this object to an array.
update()  : void
getSerializedProperties()  : array<string|int, mixed>
Get an array of properties to serialize.

Constants

HACK_MYSQL_TZ_NO_SESSION

Don't set the session TZ, for when the MySQL has no TZ data.

public mixed HACK_MYSQL_TZ_NO_SESSION = 'mysql_tz_no_session'

HACK_MYSQL_TZ_OFFSET

If MySQL hasn't got TZ data installed, convert any IANA zone names to an offset.

public mixed HACK_MYSQL_TZ_OFFSET = 'mysql_tz_offset'

HACK_NO_ENGINE_SUBSTITUTION

use session: ['sql_mode' => 'NO_ENGINE_SUBSTITUTION']

public mixed HACK_NO_ENGINE_SUBSTITUTION = 'no_engine_substitution'

HACK_SQLITE_FUNCTIONS

Insert some custom functions.

public mixed HACK_SQLITE_FUNCTIONS = 'sqlite_functions'

HACK_TIME_ZONE

use session: ['time_zone' => 'Country/Region']

public mixed HACK_TIME_ZONE = 'time_zone'

TX_ENABLE_NESTED

Enable nested transaction (via savepoints).

public mixed TX_ENABLE_NESTED = 0x4

This eliminates recursion exceptions.

TX_FORCE_COMMIT_KEYS

Force the user to provide a transaction key when calling `commit()`.

public mixed TX_FORCE_COMMIT_KEYS = 0x8

rollback() can still be called at any time without a key.

TX_STRICT_COMMIT

Require an active transaction when calling `commit()`.

public mixed TX_STRICT_COMMIT = 0x1

TX_STRICT_ROLLBACK

Require an active transaction when calling `rollback()`.

public mixed TX_STRICT_ROLLBACK = 0x2

TYPE_MSSQL

public mixed TYPE_MSSQL = 'mssql'

TYPE_MYSQL

public mixed TYPE_MYSQL = 'mysql'

TYPE_ORACLE

public mixed TYPE_ORACLE = 'oracle'

TYPE_PGSQL

public mixed TYPE_PGSQL = 'pgsql'

TYPE_SQLITE

public mixed TYPE_SQLITE = 'sqlite'

Properties

$_pdo

This is used in the Sprout compatibility layer.

public PDO|null $_pdo = null

Don't use this.

Unless maybe you're REALLY sure about it.

$attributes

Connection attributes.

public array<string|int, mixed> $attributes = []

[ PDO::ATTR_* => value ]

$cache

A caching class extending PdbCache.

public PdbCache|string|array<string|int, mixed> $cache = \karmabunny\pdb\Cache\PdbStaticCache::class

By default this is a static cache and data will only live for the duration of the request.

This can be a class string, array config, or object instance.

Example:

$config->cache = [
   PdbRedisCache::class => [
      'host' => 'localhost',
      'prefix' => 'pdb:',
   ],
];

$character_set

Connection charset, default utf8.

public string $character_set = 'utf8'

$collation

Default collation, default utf8.

public string $collation = 'utf8_unicode_ci'

$database

Database name.

public string $database = null

$dsn

Connection string override.

public string|null $dsn = null

This is required for SQLite.

E.g. /path/to/db.sqlite

$formatters

String formatters for class parameters.

public array<string|int, callable|PdbDataFormatterInterface|string> $formatters = []

The result must return a string, for example:

$formatter = function($date) {
    return date('Y-m-d H:i:s', $date);
}
$config->formatters[DateTimeInterface::class] = $formatter;

If the value is a string or array, this will be configured and asserted as a PdbDataFormatterInterface - Configure.

[class => fn]

$hacks

Driver specific hacks.

public array<string|int, string> $hacks = []

See documentation per driver for details.

$host

Connection hostname or IP address.

public string $host = null

$identity

An identity key for this connection.

public string|null $identity = null

Used for distinguishing between connections within a cache.

By default this is is a shasum of a the DSN string.

$inflector

An inflector config for pluralisation and such.

public InflectorInterface|string|array<string|int, mixed> $inflector = \karmabunny\kb\Inflector::class

$namespace

Namespace for UUIDv5 generation.

public string $namespace = \karmabunny\pdb\Pdb::UUID_NAMESPACE

$pass

Connection password.

public string $pass = null

$port

Port number, or null for default.

public int|null $port = null

$prefix

Global table prefix.

public string $prefix = 'pdb_'

Per-table prefixes can be set in the table_prefixes config.

$schema

Database schema, for Postgres.

public string $schema = 'public'

$session

Session variables per call to `connect()`.

public array<string|int, mixed> $session = []

These are driver specific.

Currently supported:

  • mysql
  • pgsql

[ varname => value ]

$socket

Treat the host as a unix socket for MySQL connections.

public string|bool $socket = true

Note, if this is set true (default) then 'localhost' is treated as a socket.

Otherwise if a string this is used as the socket path and overrides the host setting.

$sslmode

Secure mode, for Postgres.

public string $sslmode = 'prefer'

https://www.postgresql.org/docs/15/libpq-ssl.html#LIBPQ-SSL-SSLMODE-STATEMENTS

$table_prefixes

Per-table prefixes.

public array<string, string> $table_prefixes = []

These override the global prefix config.

[table => prefix]

$timeout

Connection timeout.

public int $timeout = 0

$timezone

Set the timezone for this connection.

public string|null $timezone = null

This overrides 'use_system_timezone'.

$transaction_mode

TX enum

public int $transaction_mode = self::TX_STRICT_ROLLBACK | self::TX_STRICT_COMMIT

Default: strict commit + rollback

Ideal: strict commit, force commit keys, enabled nested

$ttl

Default caching TTL, in seconds.

public int $ttl = 10

Each call to query() is able to override this. Or via the cache() modifier in a PdbQuery (recommended).

PdbReturn

seconds

$type

One of the TYPE enum.

public string $type = null
  • mysql (full support)
  • sqlite (partial)
  • pgsql (partial)
  • mssql (untested)
  • oracle (untested)

$use_system_timezone

Use the PHP timezone for `Pdb::now()`.

public bool $use_system_timezone = true

This also sets the session timezone to match.

$user

Connection username.

public string $user = null

Note that when you connect to SQL Azure, your username will be UserName@ServerId.

Methods

__construct()

public __construct([iterable<string|int, mixed> $config = [] ]) : mixed
Parameters
$config : iterable<string|int, mixed> = []

__serialize()

public __serialize() : array<string|int, mixed>
Tags
inheritdoc
Return values
array<string|int, mixed>

__unserialize()

public __unserialize(array<string|int, mixed> $serialized) : void
Parameters
$serialized : array<string|int, mixed>
Tags
inheritdoc

extraFields()

Define extra fields that you _might_ want included in the `toArray()` output.

public extraFields() : array<string|int, callable>

These are virtual fields that are not included by default like the ones defined in fields(). Instead, use the second parameter of the toArray(null, ['name']) method to specify which extra fields you want in the output.

A virtual field is defined as a pair like 'name' => callback.

Tags
see
ArrayableFields
ArrayableTrait
Return values
array<string|int, callable>

fields()

These are fields for use in `toArray()`.

public fields() : array<string|int, callable|bool>

By default this will include all public fields of the object.

Given 'name' => true a field will appear in the array output. Given false|null|0|unset|... the field will not be present.

Define a pair like 'name' => callback create a 'virtual' field. This can override existing properties or create whole new fields.

Note, if not implementing the ArrayableFields interface, this method will only define 'virtual' fields. That said, inheriting the parent fields is safe and will future-proof the code.

// Inheriting fields from the parent:
$fields = parent::fields();
$fields['hide_this_field'] = false;
unset($fields['this_also_works']);
return $fields;

// Declaring explicit fields, ignoring inherited:
return [
  'real_field' => true,
  'and_this_field' => true,
];

// Adding virtual fields:
$fields = parent::fields();
$fields['virtual_thing'] = [$this, 'getMyVirtualThing'];
$fields['inline_thing'] = function() {
   return 'hey look: ' . time();
};
return $fields;
Tags
see
ArrayableFields
ArrayableTrait
Return values
array<string|int, callable|bool>

getDsn()

The 'Data Source Name' used to connect to the database.

public getDsn() : string
Tags
throws
InvalidArgumentException
Return values
string

getFieldQuotes()

Get parameter quotes as appropriate for the underlying DBMS.

public getFieldQuotes() : array<string|int, string>

For things like fields, tables, etc.

Return values
array<string|int, string> —

[left, right]

getHack()

Is this hack enabled?

public getHack(string $name) : mixed
Parameters
$name : string

getHacks()

The hacks applied to this configuration.

public getHacks() : array<string|int, mixed>
Return values
array<string|int, mixed> —

[option => value]

getIdentity()

The identity key for this config.

public getIdentity() : string

By default this is a shasum of the DSN string. Or customize it with the 'identity' property.

This is used to provide cache keys.

Return values
string

getIterator()

public getIterator() : Traversable
Tags
inheritdoc
Return values
Traversable

getPrefixes()

Get prefixing rules.

public getPrefixes() : array<string|int, string>

The wildcard '*' is used to match all tables.

If this array is empty, no prefixing is active.

Keys are un-prefixed, i.e. ['users' => 'sprout_']

Return values
array<string|int, string> —

[ table => prefix ]

jsonSerialize()

public jsonSerialize() : mixed
Tags
inheritdoc

offsetExists()

public offsetExists(mixed $offset) : bool
Parameters
$offset : mixed
Return values
bool

offsetGet()

public offsetGet(mixed $offset) : mixed
Parameters
$offset : mixed

offsetSet()

public offsetSet(mixed $offset, mixed $value) : void
Parameters
$offset : mixed
$value : mixed

offsetUnset()

public offsetUnset(mixed $offset) : void
Parameters
$offset : mixed

toArray()

Convert this object to an array.

public toArray([array<string|int, string>|null $filter = null ][, array<string|int, string>|null $extra = null ][, bool $nulls = false ]) : array<string|int, mixed>

Specify a 'filter' to restrict the fields included in the output.

Specify an 'extra' to include additional fields if implementing the ArrayableFields interface. This will add properties that were excluded from fields() and includes any virtual fields defined in extraFields(). included in fields() and any virtual fields defined in extraFields().

Parameters
$filter : array<string|int, string>|null = null
$extra : array<string|int, string>|null = null
$nulls : bool = false
  • include null fields in the output
Return values
array<string|int, mixed>

update()

public update(iterable<string|int, mixed> $config) : void
Parameters
$config : iterable<string|int, mixed>

getSerializedProperties()

Get an array of properties to serialize.

protected getSerializedProperties() : array<string|int, mixed>

Override this to modify the behaviour of the serializer.

By default, this returns properties that are:

  • public
  • not private or protected
  • not static
  • not implementing NotSerializable
Return values
array<string|int, mixed> —

[string => value]

On this page

Search results