Properties

$max

$max : integer

Type

integer

$dsn

$dsn : string

Type

string

$loggingEnabled

$loggingEnabled : boolean

Type

boolean

$pdo

$pdo : \PDO|NULL

Type

\PDO|NULL

$affectedRows

$affectedRows : integer

Type

integer

$resultArray

$resultArray : array

Type

array

$connectInfo

$connectInfo : array

Type

array

$isConnected

$isConnected : boolean

Type

boolean

$flagUseStringOnlyBinding

$flagUseStringOnlyBinding : boolean

Type

boolean

$queryCounter

$queryCounter : integer

Type

integer

$mysqlCharset

$mysqlCharset : string

Type

string

$mysqlCollate

$mysqlCollate : string

Type

string

$stringifyFetches

$stringifyFetches : boolean

Type

boolean

$initSQL

$initSQL : string|NULL

Type

string|NULL

$initCode

$initCode : callable|NULL

Type

callable|NULL

Methods

__construct()

__construct(string|\PDO  $dsn, string  $user = NULL, string  $pass = NULL,   $options = array()) : void

Constructor. You may either specify dsn, user and password or just give an existing PDO connection.

Usage:

$driver = new RPDO( $dsn, $user, $password );

The example above illustrates how to create a driver instance from a database connection string (dsn), a username and a password. It's also possible to pass a PDO object.

Usage:

$driver = new RPDO( $existingConnection );

The second example shows how to create an RPDO instance from an existing PDO object.

Parameters

string|\PDO $dsn

database connection string

string $user

optional, username to sign in

string $pass

optional, password for connection login

$options

stringifyFetches()

stringifyFetches(boolean  $bool) 

Sets PDO in stringify fetch mode.

If set to TRUE, this method will make sure all data retrieved from the database will be fetched as a string. Default: TRUE.

To set it to FALSE...

Usage:

R::getDatabaseAdapter()->getDatabase()->stringifyFetches( FALSE );

Important! Note, this method only works if you set the value BEFORE the connection has been establish. Also, this setting ONLY works with SOME drivers. It's up to the driver to honour this setting.

Parameters

boolean $bool

getMysqlEncoding()

getMysqlEncoding(boolean  $retCol = FALSE) : string|array

Returns the best possible encoding for MySQL based on version data.

This method can be used to obtain the best character set parameters possible for your database when constructing a table creation query containing clauses like: CHARSET=... COLLATE=... This is a MySQL-specific method and not part of the driver interface.

Usage:

$charset_collate = $this->adapter->getDatabase()->getMysqlEncoding( TRUE );

Parameters

boolean $retCol

pass TRUE to return both charset/collate

Returns

string|array

setUseStringOnlyBinding()

setUseStringOnlyBinding(boolean  $yesNo) : void

Whether to bind all parameters as strings.

If set to TRUE this will cause all integers to be bound as STRINGS. This will NOT affect NULL values.

Parameters

boolean $yesNo

pass TRUE to bind all parameters as strings.

setMaxIntBind()

setMaxIntBind(integer  $max) : integer

Sets the maximum value to be bound as integer, normally this value equals PHP's MAX INT constant, however sometimes PDO driver bindings cannot bind large integers as integers.

This method allows you to manually set the max integer binding value to manage portability/compatibility issues among different PHP builds. This method will return the old value.

Parameters

integer $max

maximum value for integer bindings

Returns

integer

setInitCode()

setInitCode(callable|NULL  $code) : void

Sets initialization code to execute upon connecting.

Parameters

callable|NULL $code

code

connect()

connect() : void

Establishes a connection to the database using PHP\PDO functionality. If a connection has already been established this method will simply return directly. This method also turns on UTF8 for the database and PDO-ERRMODE-EXCEPTION as well as PDO-FETCH-ASSOC.

setPDO()

setPDO(\PDO  $pdo, array  $options = array()) : void

Directly sets PDO instance into driver.

This method might improve performance, however since the driver does not configure this instance terrible things may happen... only use this method if you are an expert on RedBeanPHP, PDO and UTF8 connections and you know your database server VERY WELL.

  • connected TRUE|FALSE (treat this instance as connected, default: TRUE)
  • setEncoding TRUE|FALSE (let RedBeanPHP set encoding for you, default: TRUE)
  • setAttributes TRUE|FALSE (let RedBeanPHP set attributes for you, default: TRUE)*
  • setDSNString TRUE|FALSE (extract DSN string from PDO instance, default: TRUE)
  • stringFetch TRUE|FALSE (whether you want to stringify fetches or not, default: TRUE)
  • runInitCode TRUE|FALSE (run init code if any, default: TRUE)

*attributes:

  • RedBeanPHP will ask database driver to throw Exceptions on errors (recommended for compatibility)
  • RedBeanPHP will ask database driver to use associative arrays when fetching (recommended for compatibility)

Parameters

\PDO $pdo

PDO instance

array $options

Options to apply

GetAll()

GetAll(string  $sql, array  $bindings = array()) : array

Runs a query and fetches results as a multi dimensional array.

Parameters

string $sql

SQL query to execute

array $bindings

list of values to bind to SQL snippet

Returns

array

GetAssocRow()

GetAssocRow(string  $sql, array  $bindings = array()) : array

Runs a query and returns results as an associative array indexed by the first column.

Parameters

string $sql

SQL query to execute

array $bindings

list of values to bind to SQL snippet

Returns

array

GetCol()

GetCol(string  $sql, array  $bindings = array()) : array

Runs a query and fetches results as a column.

Parameters

string $sql

SQL query to execute

array $bindings

list of values to bind to SQL snippet

Returns

array

GetOne()

GetOne(string  $sql, array  $bindings = array()) : mixed

Runs a query and returns results as a single cell.

Parameters

string $sql

SQL query to execute

array $bindings

list of values to bind to SQL snippet

Returns

mixed

GetCell()

GetCell(string  $sql, array  $bindings = array()) : string|NULL

Alias for getOne().

Backward compatibility.

Parameters

string $sql

SQL

array $bindings

bindings

Returns

string|NULL

GetRow()

GetRow(string  $sql, array  $bindings = array()) : array

Runs a query and returns a flat array containing the values of one row.

Parameters

string $sql

SQL query to execute

array $bindings

list of values to bind to SQL snippet

Returns

array

Execute()

Execute(string  $sql, array  $bindings = array()) : integer

Executes SQL code and allows key-value binding.

This function allows you to provide an array with values to bind to query parameters. For instance you can bind values to question marks in the query. Each value in the array corresponds to the question mark in the query that matches the position of the value in the array. You can also bind values using explicit keys, for instance array(":key"=>123) will bind the integer 123 to the key :key in the SQL. This method has no return value.

Parameters

string $sql

SQL query to execute

array $bindings

list of values to bind to SQL snippet

Returns

integer —

Affected Rows

GetInsertID()

GetInsertID() : integer

Returns the latest insert ID if driver does support this feature.

Returns

integer

GetCursor()

GetCursor(string  $sql, array  $bindings = array()) : \RedBeanPHP\Cursor

Returns a cursor-like object from the database.

Parameters

string $sql

SQL query to execute

array $bindings

list of values to bind to SQL snippet

Returns

\RedBeanPHP\Cursor

Affected_Rows()

Affected_Rows() : integer

Returns the number of rows affected by the most recent query if the currently selected driver driver supports this feature.

Returns

integer

setDebugMode()

setDebugMode(  $tf,   $logger = NULL) : void

Toggles debug mode. In debug mode the driver will print all SQL to the screen together with some information about the results.

This method is for more fine-grained control. Normally you should use the facade to start the query debugger for you. The facade will manage the object wirings necessary to use the debugging functionality.

Usage (through facade):

R::debug( TRUE ); ...rest of program... R::debug( FALSE );

The example above illustrates how to use the RedBeanPHP query debugger through the facade.

Parameters

$tf
$logger

setLogger()

setLogger(\RedBeanPHP\Logger  $logger) : self

Injects Logger object.

Sets the logger instance you wish to use.

This method is for more fine-grained control. Normally you should use the facade to start the query debugger for you. The facade will manage the object wirings necessary to use the debugging functionality.

Usage (through facade):

R::debug( TRUE ); ...rest of program... R::debug( FALSE );

The example above illustrates how to use the RedBeanPHP query debugger through the facade.

Parameters

\RedBeanPHP\Logger $logger

the logger instance to be used for logging

Returns

self

getLogger()

getLogger() : \RedBeanPHP\Logger

Gets Logger object.

Returns the currently active Logger instance.

Returns

\RedBeanPHP\Logger

StartTrans()

StartTrans() : void

Starts a transaction.

CommitTrans()

CommitTrans() : void

Commits a transaction.

FailTrans()

FailTrans() : void

Rolls back a transaction.

getDatabaseType()

getDatabaseType() : string

Returns the name of database driver for PDO.

Uses the PDO attribute DRIVER NAME to obtain the name of the PDO driver. Use this method to identify the current PDO driver used to provide access to the database. Example of a database driver string:

mysql

Usage:

echo R::getDatabaseAdapter()->getDatabase()->getDatabaseType();

The example above prints the current database driver string to stdout.

Note that this is a driver-specific method, not part of the driver interface. This method might not be available in other drivers since it relies on PDO.

Returns

string

getDatabaseVersion()

getDatabaseVersion() : mixed

Returns the version identifier string of the database client.

This method can be used to identify the currently installed database client. Note that this method will also establish a connection (because this is required to obtain the version information).

Example of a version string:

mysqlnd 5.0.12-dev - 20150407 - $Id: b5c5906d452ec590732a93b051f3827e02749b83 $

Usage:

echo R::getDatabaseAdapter()->getDatabase()->getDatabaseVersion();

The example above will print the version string to stdout.

Note that this is a driver-specific method, not part of the driver interface. This method might not be available in other drivers since it relies on PDO.

To obtain the database server version, use getDatabaseServerVersion() instead.

Returns

mixed

getPDO()

getPDO() : \PDO

Returns the underlying PHP PDO instance.

For some low-level database operations you'll need access to the PDO object. Not that this method is only available in RPDO and other PDO based database drivers for RedBeanPHP. Other drivers may not have a method like this. The following example demonstrates how to obtain a reference to the PDO instance from the facade:

Usage:

$pdo = R::getDatabaseAdapter()->getDatabase()->getPDO();

Returns

\PDO

close()

close() : void

Closes the database connection.

While database connections are closed automatically at the end of the PHP script, closing database connections is generally recommended to improve performance. Closing a database connection will immediately return the resources to PHP.

Usage:

R::setup( ... ); ... do stuff ... R::close();

isConnected()

isConnected() : boolean

Returns TRUE if the current PDO instance is connected.

Returns

boolean

setEnableLogging()

setEnableLogging(boolean  $enable) : self

Toggles logging, enables or disables logging.

Parameters

boolean $enable

TRUE to enable logging

Returns

self

resetCounter()

resetCounter() : self

Resets the query counter.

The query counter can be used to monitor the number of database queries that have been processed according to the database driver. You can use this to monitor the number of queries required to render a page.

Usage:

R::resetQueryCount(); echo R::getQueryCount() . ' queries processed.';

Returns

self

getQueryCount()

getQueryCount() : integer

Returns the number of SQL queries processed.

This method returns the number of database queries that have been processed according to the database driver. You can use this to monitor the number of queries required to render a page.

Usage:

echo R::getQueryCount() . ' queries processed.';

Returns

integer

getIntegerBindingMax()

getIntegerBindingMax() : integer

Returns the maximum value treated as integer parameter binding.

This method is mainly for testing purposes but it can help you solve some issues relating to integer bindings.

Returns

integer

setInitQuery()

setInitQuery(string  $sql) : self

Sets a query to be executed upon connecting to the database.

This method provides an opportunity to configure the connection to a database through an SQL-based interface. Objects can provide an SQL string to be executed upon establishing a connection to the database. This has been used to solve issues with default foreign key settings in SQLite3 for instance, see Github issues:

545 and #548.

Parameters

string $sql

SQL query to run upon connecting to database

Returns

self

DatabaseServerVersion()

DatabaseServerVersion() : string

Returns the version string from the database server.

Returns

string

bindParams()

bindParams(\PDOStatement  $statement, array  $bindings) : void

Binds parameters. This method binds parameters to a PDOStatement for Query Execution. This method binds parameters as NULL, INTEGER or STRING and supports both named keys and question mark keys.

Parameters

\PDOStatement $statement

PDO Statement instance

array $bindings

values that need to get bound to the statement

runQuery()

runQuery(string  $sql, array  $bindings, array  $options = array()) : mixed

This method runs the actual SQL query and binds a list of parameters to the query.

slots. The result of the query will be stored in the protected property $rs (always array). The number of rows affected (result of rowcount, if supported by database) is stored in protected property $affectedRows. If the debug flag is set this function will send debugging output to screen buffer.

Parameters

string $sql

the SQL string to be send to database server

array $bindings

the values that need to get bound to the query slots

array $options

Throws

\RedBeanPHP\RedException\SQL

Returns

mixed

setEncoding()

setEncoding() : void

Try to fix MySQL character encoding problems.

MySQL < 5.5.3 does not support proper 4 byte unicode but they seem to have added it with version 5.5.3 under a different label: utf8mb4. We try to select the best possible charset based on your version data.

hasCap()

hasCap(string  $db_cap) : integer|false

Determine if a database supports a particular feature.

Currently this function can be used to detect the following features:

  • utf8mb4
  • utf8mb4 520

Usage:

$this->hasCap( 'utf8mb4_520' );

By default, RedBeanPHP uses this method under the hood to make sure you use the latest UTF8 encoding possible for your database.

Parameters

string $db_cap

identifier of database capability

Returns

integer|false —

Whether the database feature is supported, FALSE otherwise.