Propel API
Class

PropelPDO

class PropelPDO extends PDO

PDO connection subclass that provides the basic fixes to PDO that are required by Propel.

This class was designed to work around the limitation in PDO where attempting to begin
a transaction when one has already been begun will trigger a PDOException. Propel
relies on the ability to create nested transactions, even if the underlying layer
simply ignores these (because it doesn't support nested transactions).

The changes that this class makes to the underlying API include the addition of the
getNestedTransactionDepth() and isInTransaction() and the fact that beginTransaction()
will no longer throw a PDOException (or trigger an error) if a transaction is already
in-progress.

Constants

PROPEL_ATTR_CACHE_PREPARES

Attribute to use to set whether to cache prepared statements.

DEFAULT_SLOW_THRESHOLD

DEFAULT_ONLYSLOW_ENABLED

Properties

boolean $useDebug Whether or not the debug is enabled

Methods

__construct(string $dsn, string $username = null, string $password = null, array $driver_options = array())

Creates a PropelPDO instance representing a connection to a database.

setConfiguration(PropelConfiguration $configuration)

Inject the runtime configuration

PropelConfiguration getConfiguration()

Get the runtime configuration

int getNestedTransactionCount()

Gets the current transaction depth.

boolean isInTransaction()

Is this PDO connection currently in-transaction? This is equivalent to asking whether the current nested transaction count is greater than 0.

boolean isCommitable()

Check whether the connection contains a transaction that can be committed.

beginTransaction()

Overrides PDO::beginTransaction() to prevent errors due to already-in-progress transaction.

commit()

Overrides PDO::commit() to only commit the transaction if we are in the outermost transaction nesting level.

boolean rollBack()

Overrides PDO::rollBack() to only rollback the transaction if we are in the outermost transaction nesting level

boolean forceRollBack()

Rollback the whole transaction, even if this is a nested rollback and reset the nested transaction count to 0.

setAttribute(int $attribute, mixed $value)

Sets a connection attribute.

getAttribute(int $attribute)

Gets a connection attribute.

PDOStatement prepare(string $sql, array $driver_options = array())

Prepares a statement for execution and returns a statement object.

int exec($sql)

Execute an SQL statement and return the number of affected rows.

PDOStatement query()

Executes an SQL statement, returning a result set as a PDOStatement object.

clearStatementCache()

Clears any stored prepared statements for this connection.

int getQueryCount()

Returns the number of queries this DebugPDO instance has performed on the database connection.

int incrementQueryCount()

Increments the number of queries performed by this DebugPDO instance.

string getLastExecutedQuery()

Get the SQL code for the latest query executed by Propel

setLastExecutedQuery(string $query)

Set the SQL code for the latest query executed by Propel

useDebug($value = true)

Enable or disable the query debug features

setLogLevel(int $level)

Sets the logging level to use for logging method calls and SQL statements.

setLogger(BasicLogger $logger)

Sets a logger to use.

BasicLogger getLogger()

Gets the logger in use.

log(string $msg, int $level = null, string $methodName = null, array $debugSnapshot = null)

Logs the method call or SQL using the Propel::log() method or a registered logger class.

array getDebugSnapshot()

Returns a snapshot of the current values of some functions useful in debugging.

__destruct()

If so configured, makes an entry to the log of the state of this object just prior to its destruction.

Details

at line 133
public __construct(string $dsn, string $username = null, string $password = null, array $driver_options = array())

Creates a PropelPDO instance representing a connection to a database.

.
If so configured, specifies a custom PDOStatement class and makes an entry
to the log with the state of this object just after its initialization.
Add PropelPDO::__construct to $defaultLogMethods to see this message

Parameters

string $dsn Connection DSN.
string $username (optional) The user name for the DSN string.
string $password (optional) The password for the DSN string.
array $driver_options (optional) A key=>value array of driver-specific connection options.

Exceptions

PDOException if there is an error during connection initialization.

at line 152
public setConfiguration(PropelConfiguration $configuration)

Inject the runtime configuration

Parameters

PropelConfiguration $configuration

at line 162
public PropelConfiguration getConfiguration()

Get the runtime configuration

Return Value

PropelConfiguration

at line 174
public int getNestedTransactionCount()

Gets the current transaction depth.

Return Value

int

at line 194
public boolean isInTransaction()

Is this PDO connection currently in-transaction? This is equivalent to asking whether the current nested transaction count is greater than 0.

Return Value

boolean

at line 205
public boolean isCommitable()

Check whether the connection contains a transaction that can be committed.

To be used in an evironment where Propelexceptions are caught.

Return Value

boolean True if the connection is in a committable transaction

at line 213
public beginTransaction()

Overrides PDO::beginTransaction() to prevent errors due to already-in-progress transaction.

at line 231
public commit()

Overrides PDO::commit() to only commit the transaction if we are in the outermost transaction nesting level.

at line 256
public boolean rollBack()

Overrides PDO::rollBack() to only rollback the transaction if we are in the outermost transaction nesting level

Return Value

boolean Whether operation was successful.

at line 279
public boolean forceRollBack()

Rollback the whole transaction, even if this is a nested rollback and reset the nested transaction count to 0.

Return Value

boolean Whether operation was successful.

at line 307
public setAttribute(int $attribute, mixed $value)

Sets a connection attribute.

This is overridden here to provide support for setting Propel-specific attributes
too.

Parameters

int $attribute The attribute to set (e.g. PropelPDO::PROPEL_ATTR_CACHE_PREPARES).
mixed $value The attribute value.

at line 326
public getAttribute(int $attribute)

Gets a connection attribute.

This is overridden here to provide support for setting Propel-specific attributes
too.

Parameters

int $attribute The attribute to get (e.g. PropelPDO::PROPEL_ATTR_CACHE_PREPARES).

at line 349
public PDOStatement prepare(string $sql, array $driver_options = array())

Prepares a statement for execution and returns a statement object.

Overrides PDO::prepare() in order to:
- Add logging and query counting if logging is true.
- Add query caching support if the PropelPDO::PROPEL_ATTR_CACHE_PREPARES was set to true.

Parameters

string $sql This must be a valid SQL statement for the target database server.
array $driver_options One or more key => value pairs to set attribute values for the PDOStatement object that this method returns.

Return Value

PDOStatement

at line 380
public int exec($sql)

Execute an SQL statement and return the number of affected rows.

Overrides PDO::exec() to log queries when required

Parameters

$sql

Return Value

int

at line 406
public PDOStatement query()

Executes an SQL statement, returning a result set as a PDOStatement object.

Despite its signature here, this method takes a variety of parameters.

Overrides PDO::query() to log queries when required

Return Value

PDOStatement

See also

http://php.net/manual/en/pdo.query.php for a description of the possible parameters.

at line 432
public clearStatementCache()

Clears any stored prepared statements for this connection.

at line 462
public int getQueryCount()

Returns the number of queries this DebugPDO instance has performed on the database connection.

When using DebugPDOStatement as the statement class, any queries by DebugPDOStatement instances
are counted as well.

Return Value

int

Exceptions

PropelException if persistent connection is used (since unable to override PDOStatement in that case).

at line 478
public int incrementQueryCount()

Increments the number of queries performed by this DebugPDO instance.

Returns the original number of queries (ie the value of $this->queryCount before calling this method).

Return Value

int

at line 488
public string getLastExecutedQuery()

Get the SQL code for the latest query executed by Propel

Return Value

string Executable SQL code

at line 498
public setLastExecutedQuery(string $query)

Set the SQL code for the latest query executed by Propel

Parameters

string $query Executable SQL code

at line 508
public useDebug($value = true)

Enable or disable the query debug features

Parameters

$value

at line 527
public setLogLevel(int $level)

Sets the logging level to use for logging method calls and SQL statements.

Parameters

int $level Value of one of the Propel::LOG_* class constants.

at line 539
public setLogger(BasicLogger $logger)

Sets a logger to use.

The logger will be used by this class to log various method calls and their properties.

Parameters

BasicLogger $logger A Logger with an API compatible with BasicLogger (or PEAR Log).

at line 549
public BasicLogger getLogger()

Gets the logger in use.

Return Value

BasicLogger $logger A Logger with an API compatible with BasicLogger (or PEAR Log).

at line 565
public log(string $msg, int $level = null, string $methodName = null, array $debugSnapshot = null)

Logs the method call or SQL using the Propel::log() method or a registered logger class.

Parameters

string $msg Message to log.
int $level (optional) Log level to use; will use self::setLogLevel() specified level by default.
string $methodName (optional) Name of the method whose execution is being logged.
array $debugSnapshot (optional) Previous return value from self::getDebugSnapshot().

See also

self::setLogger()

at line 611
public array getDebugSnapshot()

Returns a snapshot of the current values of some functions useful in debugging.

Return Value

array

at line 742
public __destruct()

If so configured, makes an entry to the log of the state of this object just prior to its destruction.

Add PropelPDO::__destruct to $defaultLogMethods to see this message

See also

self::log()