PHP Interface Doctrine\DBAL\Connection

Exibir arquivo Open project: doctrine/dbal Interface Usage Examples

Protected Properties

Property Type Description
$_config Doctrine\DBAL\Configuration
$_conn Doctrine\DBAL\Driver\Connection The wrapped driver connection.
$_driver Doctrine\DBAL\Driver The used DBAL driver.
$_eventManager Doctrine\Common\EventManager
$_expr Doctrine\DBAL\Query\Expression\ExpressionBuilder
$_schemaManager Doctrine\DBAL\Schema\AbstractSchemaManager The schema manager.
$defaultFetchMode integer

Public Methods

Method Description
__construct ( array $params, Doctrine\DBAL\Driver $driver, Doctrine\DBAL\Configuration $config = null, Doctrine\Common\EventManager $eventManager = null ) Initializes a new instance of the Connection class.
beginTransaction ( ) : void Starts a transaction by suspending auto-commit mode.
close ( ) : void Closes the connection.
commit ( ) : void Commits the current transaction.
connect ( ) : boolean Establishes the connection with the database.
convertToDatabaseValue ( mixed $value, string $type ) : mixed Converts a given value to its database representation according to the conversion rules of a specific DBAL mapping type.
convertToPHPValue ( mixed $value, string $type ) : mixed Converts a given value to its PHP representation according to the conversion rules of a specific DBAL mapping type.
createQueryBuilder ( ) : Doctrine\DBAL\Query\QueryBuilder Creates a new instance of a SQL query builder.
createSavepoint ( string $savepoint ) : void Creates a new savepoint.
delete ( string $tableExpression, array $identifier, array $types = [] ) : integer Executes an SQL DELETE statement on a table.
errorCode ( ) : integer Fetches the SQLSTATE associated with the last database operation.
errorInfo ( ) : array Fetches extended error information associated with the last database operation.
exec ( string $statement ) : integer Executes an SQL statement and return the number of affected rows.
executeCacheQuery ( string $query, array $params, array $types, Doctrine\DBAL\Cache\QueryCacheProfile $qcp ) : Doctrine\DBAL\Driver\ResultStatement Executes a caching query.
executeQuery ( string $query, array $params = [], array $types = [], Doctrine\DBAL\Cache\QueryCacheProfile $qcp = null ) : Doctrine\DBAL\Driver\Statement Executes an, optionally parametrized, SQL query.
executeUpdate ( string $query, array $params = [], array $types = [] ) : integer Executes an SQL INSERT/UPDATE/DELETE query with the given parameters and returns the number of affected rows.
fetchAll ( string $sql, array $params = [], array $types = [] ) : array Prepares and executes an SQL query and returns the result as an associative array.
fetchArray ( string $statement, array $params = [], array $types = [] ) : array | boolean Prepares and executes an SQL query and returns the first row of the result as a numerically indexed array.
fetchAssoc ( string $statement, array $params = [], array $types = [] ) : array | boolean Prepares and executes an SQL query and returns the first row of the result as an associative array.
fetchColumn ( string $statement, array $params = [], integer $column, array $types = [] ) : mixed | boolean Prepares and executes an SQL query and returns the value of a single column of the first row of the result.
getConfiguration ( ) : Doctrine\DBAL\Configuration Gets the Configuration used by the Connection.
getDatabase ( ) : string Gets the name of the database this Connection is connected to.
getDatabasePlatform ( ) : Doctrine\DBAL\Platforms\AbstractPlatform Gets the DatabasePlatform for the connection.
getDriver ( ) : Doctrine\DBAL\Driver Gets the DBAL driver instance.
getEventManager ( ) : Doctrine\Common\EventManager Gets the EventManager used by the Connection.
getExpressionBuilder ( ) : Doctrine\DBAL\Query\Expression\ExpressionBuilder Gets the ExpressionBuilder for the connection.
getHost ( ) : string | null Gets the hostname of the currently connected database.
getNestTransactionsWithSavepoints ( ) : boolean Gets if nested transactions should use savepoints.
getParams ( ) : array Gets the parameters used during instantiation.
getPassword ( ) : string | null Gets the password used by this connection.
getPort ( ) : mixed Gets the port of the currently connected database.
getSchemaManager ( ) : Doctrine\DBAL\Schema\AbstractSchemaManager Gets the SchemaManager that can be used to inspect or change the database schema through the connection.
getTransactionIsolation ( ) : integer Gets the currently active transaction isolation level.
getTransactionNestingLevel ( ) : integer Returns the current transaction nesting level.
getUsername ( ) : string | null Gets the username used by this connection.
getWrappedConnection ( ) : Doctrine\DBAL\Driver\Connection Gets the wrapped driver connection.
insert ( string $tableExpression, array $data, array $types = [] ) : integer Inserts a table row with specified data.
isAutoCommit ( ) : boolean Returns the current auto-commit mode for this connection.
isConnected ( ) : boolean Whether an actual connection to the database is established.
isRollbackOnly ( ) : boolean Checks whether the current transaction is marked for rollback only.
isTransactionActive ( ) : boolean Checks whether a transaction is currently active.
lastInsertId ( string | null $seqName = null ) : string Returns the ID of the last inserted row, or the last value from a sequence object, depending on the underlying driver.
ping ( ) : boolean Ping the server
prepare ( string $statement ) : Statement Prepares an SQL statement.
project ( string $query, array $params, Closur\Closure $function ) : array Executes an, optionally parametrized, SQL query and returns the result, applying a given projection/transformation function on each row of the result.
query ( ) : Doctrine\DBAL\Driver\Statement Executes an SQL statement, returning a result set as a Statement object.
quote ( mixed $input, string | null $type = null ) : string Quotes a given input parameter.
quoteIdentifier ( string $str ) : string Quotes a string so it can be safely used as a table or column name, even if it is a reserved name.
releaseSavepoint ( string $savepoint ) : void Releases the given savepoint.
resolveParams ( array $params, array $types ) : array Resolves the parameters to a format which can be displayed.
rollBack ( ) Cancels any database changes done during the current transaction.
rollbackSavepoint ( string $savepoint ) : void Rolls back to the given savepoint.
setAutoCommit ( boolean $autoCommit ) Sets auto-commit mode for this connection.
setFetchMode ( integer $fetchMode ) : void Sets the fetch mode.
setNestTransactionsWithSavepoints ( boolean $nestTransactionsWithSavepoints ) : void Sets if nested transactions should use savepoints.
setRollbackOnly ( ) : void Marks the current transaction so that the only possible outcome for the transaction to be rolled back.
setTransactionIsolation ( integer $level ) : integer Sets the transaction isolation level.
transactional ( Closur\Closure $func ) : mixed Executes a function in a transaction.
update ( string $tableExpression, array $data, array $identifier, array $types = [] ) : integer Executes an SQL UPDATE statement on a table.

Protected Methods

Method Description
_getNestedTransactionSavePointName ( ) : mixed Returns the savepoint name to use for nested transactions are false if they are not supported "savepointFormat" parameter is not set

Private Methods

Method Description
_bindTypedValues ( Doctrine\DBAL\Driver\Statement $stmt, array $params, array $types ) : void Binds a set of parameters, some or all of which are typed with a PDO binding type or DBAL mapping type, to a given statement.
commitAll ( ) Commits all current nesting transactions.
detectDatabasePlatform ( ) Detects and sets the database platform.
extractTypeValues ( array $data, array $types ) : array Extract ordered type list from two associate key lists of data and types.
getBindingInfo ( mixed $value, mixed $type ) : array Gets the binding type of a given type. The given type can be a PDO or DBAL mapping type.
getDatabasePlatformVersion ( ) : string | null Returns the version of the related platform if applicable.

Method Details

__construct() public method

Initializes a new instance of the Connection class.
public __construct ( array $params, Doctrine\DBAL\Driver $driver, Doctrine\DBAL\Configuration $config = null, Doctrine\Common\EventManager $eventManager = null )
$params array The connection parameters.
$driver Doctrine\DBAL\Driver The driver to use.
$config Doctrine\DBAL\Configuration The configuration, optional.
$eventManager Doctrine\Common\EventManager The event manager, optional.

_getNestedTransactionSavePointName() protected method

Returns the savepoint name to use for nested transactions are false if they are not supported "savepointFormat" parameter is not set
protected _getNestedTransactionSavePointName ( ) : mixed
return mixed A string with the savepoint name or false.

beginTransaction() public method

Starts a transaction by suspending auto-commit mode.
public beginTransaction ( ) : void
return void

close() public method

Closes the connection.
public close ( ) : void
return void

commit() public method

Commits the current transaction.
public commit ( ) : void
return void

connect() public method

Establishes the connection with the database.
public connect ( ) : boolean
return boolean TRUE if the connection was successfully established, FALSE if the connection is already open.

convertToDatabaseValue() public method

Converts a given value to its database representation according to the conversion rules of a specific DBAL mapping type.
public convertToDatabaseValue ( mixed $value, string $type ) : mixed
$value mixed The value to convert.
$type string The name of the DBAL mapping type.
return mixed The converted value.

convertToPHPValue() public method

Converts a given value to its PHP representation according to the conversion rules of a specific DBAL mapping type.
public convertToPHPValue ( mixed $value, string $type ) : mixed
$value mixed The value to convert.
$type string The name of the DBAL mapping type.
return mixed The converted type.

createQueryBuilder() public method

Creates a new instance of a SQL query builder.
public createQueryBuilder ( ) : Doctrine\DBAL\Query\QueryBuilder
return Doctrine\DBAL\Query\QueryBuilder

createSavepoint() public method

Creates a new savepoint.
public createSavepoint ( string $savepoint ) : void
$savepoint string The name of the savepoint to create.
return void

delete() public method

Table expression and columns are not escaped and are not safe for user-input.
public delete ( string $tableExpression, array $identifier, array $types = [] ) : integer
$tableExpression string The expression of the table on which to delete.
$identifier array The deletion criteria. An associative array containing column-value pairs.
$types array The types of identifiers.
return integer The number of affected rows.

errorCode() public method

Fetches the SQLSTATE associated with the last database operation.
public errorCode ( ) : integer
return integer The last error code.

errorInfo() public method

Fetches extended error information associated with the last database operation.
public errorInfo ( ) : array
return array The last error information.

exec() public method

Executes an SQL statement and return the number of affected rows.
public exec ( string $statement ) : integer
$statement string
return integer The number of affected rows.

executeCacheQuery() public method

Executes a caching query.
public executeCacheQuery ( string $query, array $params, array $types, Doctrine\DBAL\Cache\QueryCacheProfile $qcp ) : Doctrine\DBAL\Driver\ResultStatement
$query string The SQL query to execute.
$params array The parameters to bind to the query, if any.
$types array The types the previous parameters are in.
$qcp Doctrine\DBAL\Cache\QueryCacheProfile The query cache profile.
return Doctrine\DBAL\Driver\ResultStatement

executeQuery() public method

If the query is parametrized, a prepared statement is used. If an SQLLogger is configured, the execution is logged.
public executeQuery ( string $query, array $params = [], array $types = [], Doctrine\DBAL\Cache\QueryCacheProfile $qcp = null ) : Doctrine\DBAL\Driver\Statement
$query string The SQL query to execute.
$params array The parameters to bind to the query, if any.
$types array The types the previous parameters are in.
$qcp Doctrine\DBAL\Cache\QueryCacheProfile The query cache profile, optional.
return Doctrine\DBAL\Driver\Statement The executed statement.

executeUpdate() public method

This method supports PDO binding types as well as DBAL mapping types.
public executeUpdate ( string $query, array $params = [], array $types = [] ) : integer
$query string The SQL query.
$params array The query parameters.
$types array The parameter types.
return integer The number of affected rows.

fetchAll() public method

Prepares and executes an SQL query and returns the result as an associative array.
public fetchAll ( string $sql, array $params = [], array $types = [] ) : array
$sql string The SQL query.
$params array The query parameters.
$types array The query parameter types.
return array

fetchArray() public method

Prepares and executes an SQL query and returns the first row of the result as a numerically indexed array.
public fetchArray ( string $statement, array $params = [], array $types = [] ) : array | boolean
$statement string The SQL query to be executed.
$params array The prepared statement params.
$types array The query parameter types.
return array | boolean False is returned if no rows are found.

fetchAssoc() public method

Prepares and executes an SQL query and returns the first row of the result as an associative array.
public fetchAssoc ( string $statement, array $params = [], array $types = [] ) : array | boolean
$statement string The SQL query.
$params array The query parameters.
$types array The query parameter types.
return array | boolean False is returned if no rows are found.

fetchColumn() public method

Prepares and executes an SQL query and returns the value of a single column of the first row of the result.
public fetchColumn ( string $statement, array $params = [], integer $column, array $types = [] ) : mixed | boolean
$statement string The SQL query to be executed.
$params array The prepared statement params.
$column integer The 0-indexed column number to retrieve.
$types array The query parameter types.
return mixed | boolean False is returned if no rows are found.

getConfiguration() public method

Gets the Configuration used by the Connection.
public getConfiguration ( ) : Doctrine\DBAL\Configuration
return Doctrine\DBAL\Configuration

getDatabase() public method

Gets the name of the database this Connection is connected to.
public getDatabase ( ) : string
return string

getDatabasePlatform() public method

Gets the DatabasePlatform for the connection.
public getDatabasePlatform ( ) : Doctrine\DBAL\Platforms\AbstractPlatform
return Doctrine\DBAL\Platforms\AbstractPlatform

getDriver() public method

Gets the DBAL driver instance.
public getDriver ( ) : Doctrine\DBAL\Driver
return Doctrine\DBAL\Driver

getEventManager() public method

Gets the EventManager used by the Connection.
public getEventManager ( ) : Doctrine\Common\EventManager
return Doctrine\Common\EventManager

getExpressionBuilder() public method

Gets the ExpressionBuilder for the connection.
public getExpressionBuilder ( ) : Doctrine\DBAL\Query\Expression\ExpressionBuilder
return Doctrine\DBAL\Query\Expression\ExpressionBuilder

getHost() public method

Gets the hostname of the currently connected database.
public getHost ( ) : string | null
return string | null

getNestTransactionsWithSavepoints() public method

Gets if nested transactions should use savepoints.

getParams() public method

Gets the parameters used during instantiation.
public getParams ( ) : array
return array

getPassword() public method

Gets the password used by this connection.
public getPassword ( ) : string | null
return string | null

getPort() public method

Gets the port of the currently connected database.
public getPort ( ) : mixed
return mixed

getSchemaManager() public method

Gets the SchemaManager that can be used to inspect or change the database schema through the connection.
public getSchemaManager ( ) : Doctrine\DBAL\Schema\AbstractSchemaManager
return Doctrine\DBAL\Schema\AbstractSchemaManager

getTransactionIsolation() public method

Gets the currently active transaction isolation level.
public getTransactionIsolation ( ) : integer
return integer The current transaction isolation level.

getTransactionNestingLevel() public method

Returns the current transaction nesting level.
public getTransactionNestingLevel ( ) : integer
return integer The nesting level. A value of 0 means there's no active transaction.

getUsername() public method

Gets the username used by this connection.
public getUsername ( ) : string | null
return string | null

getWrappedConnection() public method

Gets the wrapped driver connection.
public getWrappedConnection ( ) : Doctrine\DBAL\Driver\Connection
return Doctrine\DBAL\Driver\Connection

insert() public method

Table expression and columns are not escaped and are not safe for user-input.
public insert ( string $tableExpression, array $data, array $types = [] ) : integer
$tableExpression string The expression of the table to insert data into, quoted or unquoted.
$data array An associative array containing column-value pairs.
$types array Types of the inserted data.
return integer The number of affected rows.

isAutoCommit() public method

Returns the current auto-commit mode for this connection.
See also: setAutoCommit
public isAutoCommit ( ) : boolean
return boolean True if auto-commit mode is currently enabled for this connection, false otherwise.

isConnected() public method

Whether an actual connection to the database is established.
public isConnected ( ) : boolean
return boolean

isRollbackOnly() public method

Checks whether the current transaction is marked for rollback only.
public isRollbackOnly ( ) : boolean
return boolean

isTransactionActive() public method

Checks whether a transaction is currently active.
public isTransactionActive ( ) : boolean
return boolean TRUE if a transaction is currently active, FALSE otherwise.

lastInsertId() public method

Note: This method may not return a meaningful or consistent result across different drivers, because the underlying database may not even support the notion of AUTO_INCREMENT/IDENTITY columns or sequences.
public lastInsertId ( string | null $seqName = null ) : string
$seqName string | null Name of the sequence object from which the ID should be returned.
return string A string representation of the last inserted ID.

ping() public method

When the server is not available the method returns FALSE. It is responsibility of the developer to handle this case and abort the request or reconnect manually:
public ping ( ) : boolean
return boolean

prepare() public method

Prepares an SQL statement.
public prepare ( string $statement ) : Statement
$statement string The SQL statement to prepare.
return Statement The prepared statement.

project() public method

Executes an, optionally parametrized, SQL query and returns the result, applying a given projection/transformation function on each row of the result.
public project ( string $query, array $params, Closur\Closure $function ) : array
$query string The SQL query to execute.
$params array The parameters, if any.
$function Closur\Closure The transformation function that is applied on each row. The function receives a single parameter, an array, that represents a row of the result set.
return array The projected result of the query.

query() public method

Executes an SQL statement, returning a result set as a Statement object.
public query ( ) : Doctrine\DBAL\Driver\Statement
return Doctrine\DBAL\Driver\Statement

quote() public method

Quotes a given input parameter.
public quote ( mixed $input, string | null $type = null ) : string
$input mixed The parameter to be quoted.
$type string | null The type of the parameter.
return string The quoted parameter.

quoteIdentifier() public method

Delimiting style depends on the underlying database platform that is being used. NOTE: Just because you CAN use quoted identifiers does not mean you SHOULD use them. In general, they end up causing way more problems than they solve.
public quoteIdentifier ( string $str ) : string
$str string The name to be quoted.
return string The quoted name.

releaseSavepoint() public method

Releases the given savepoint.
public releaseSavepoint ( string $savepoint ) : void
$savepoint string The name of the savepoint to release.
return void

resolveParams() public method

Resolves the parameters to a format which can be displayed.
public resolveParams ( array $params, array $types ) : array
$params array
$types array
return array

rollBack() public method

Cancels any database changes done during the current transaction.
public rollBack ( )

rollbackSavepoint() public method

Rolls back to the given savepoint.
public rollbackSavepoint ( string $savepoint ) : void
$savepoint string The name of the savepoint to rollback to.
return void

setAutoCommit() public method

If a connection is in auto-commit mode, then all its SQL statements will be executed and committed as individual transactions. Otherwise, its SQL statements are grouped into transactions that are terminated by a call to either the method commit or the method rollback. By default, new connections are in auto-commit mode. NOTE: If this method is called during a transaction and the auto-commit mode is changed, the transaction is committed. If this method is called and the auto-commit mode is not changed, the call is a no-op.
See also: isAutoCommit
public setAutoCommit ( boolean $autoCommit )
$autoCommit boolean True to enable auto-commit mode; false to disable it.

setFetchMode() public method

Sets the fetch mode.
public setFetchMode ( integer $fetchMode ) : void
$fetchMode integer
return void

setNestTransactionsWithSavepoints() public method

Sets if nested transactions should use savepoints.
public setNestTransactionsWithSavepoints ( boolean $nestTransactionsWithSavepoints ) : void
$nestTransactionsWithSavepoints boolean
return void

setRollbackOnly() public method

Marks the current transaction so that the only possible outcome for the transaction to be rolled back.
public setRollbackOnly ( ) : void
return void

setTransactionIsolation() public method

Sets the transaction isolation level.
public setTransactionIsolation ( integer $level ) : integer
$level integer The level to set.
return integer

transactional() public method

The function gets passed this Connection instance as an (optional) parameter. If an exception occurs during execution of the function or transaction commit, the transaction is rolled back and the exception re-thrown.
public transactional ( Closur\Closure $func ) : mixed
$func Closur\Closure The function to execute transactionally.
return mixed The value returned by $func

update() public method

Table expression and columns are not escaped and are not safe for user-input.
public update ( string $tableExpression, array $data, array $identifier, array $types = [] ) : integer
$tableExpression string The expression of the table to update quoted or unquoted.
$data array An associative array containing column-value pairs.
$identifier array The update criteria. An associative array containing column-value pairs.
$types array Types of the merged $data and $identifier arrays in that order.
return integer The number of affected rows.

Property Details

$_config protected_oe property

protected Configuration,Doctrine\DBAL $_config
return Doctrine\DBAL\Configuration

$_conn protected_oe property

The wrapped driver connection.
protected Connection,Doctrine\DBAL\Driver $_conn
return Doctrine\DBAL\Driver\Connection

$_driver protected_oe property

The used DBAL driver.
protected Driver,Doctrine\DBAL $_driver
return Doctrine\DBAL\Driver

$_eventManager protected_oe property

protected EventManager,Doctrine\Common $_eventManager
return Doctrine\Common\EventManager

$_expr protected_oe property

protected ExpressionBuilder,Doctrine\DBAL\Query\Expression $_expr
return Doctrine\DBAL\Query\Expression\ExpressionBuilder

$_schemaManager protected_oe property

The schema manager.
protected AbstractSchemaManager,Doctrine\DBAL\Schema $_schemaManager
return Doctrine\DBAL\Schema\AbstractSchemaManager

$defaultFetchMode protected_oe property

protected int $defaultFetchMode
return integer