PHP Class Contao\Database

The class is responsible for connecting to the database, listing tables and fields, handling transactions and locking tables. It also creates the related Database\Statement and Database\Result objects. Usage: $db = Database::getInstance(); $stmt = $db->prepare("SELECT * FROM tl_user WHERE id=?"); $res = $stmt->execute(4);
Mostrar archivo Open project: contao/core-bundle Class Usage Examples

Protected Properties

Property Type Description
$arrCache array Cache
$arrInstances array Object instances (Singleton)
$blnDisableAutocommit boolean Disable autocommit
$resConnection Doctrine\DBAL\Connection Connection ID

Public Methods

Method Description
__clone ( ) Prevent cloning of the object (Singleton)
__destruct ( ) Close the database connection
__get ( string $strKey ) : string | null Return an object property
beginTransaction ( ) Begin a transaction
commitTransaction ( ) Commit a transaction
execute ( string $strQuery ) : Result | object Execute a query and return a Database\Result object
executeCached ( string $strQuery ) : Result | object Always execute the query and add or replace an existing cache entry
executeUncached ( string $strQuery ) : Result | object Execute a query and do not cache the result
fieldExists ( string $strField, string $strTable, boolean $blnNoCache = false ) : boolean Determine if a particular column exists
findInSet ( string $strKey, mixed $varSet, boolean $blnIsField = false ) : string Auto-generate a FIND_IN_SET() statement
getChildRecords ( mixed $arrParentIds, string $strTable, boolean $blnSorting = false, array $arrReturn = [], string $strWhere = '' ) : array Return the IDs of all child records of a particular record (see #2475)
getFieldNames ( string $strTable, boolean $blnNoCache = false ) : array Return the field names of a particular table as array
getInstance ( array $arrCustomConfig = null ) : Database Instantiate the Database object (Factory)
getNextId ( string $strTable ) : integer Return the next autoincrement ID of a table
getParentRecords ( integer $intId, string $strTable ) : array Return the IDs of all parent records of a particular record
getSizeOf ( string $strTable ) : integer Return the table size in bytes
getUuid ( ) : string Return a universal unique identifier
indexExists ( string $strName, string $strTable, boolean $blnNoCache = false ) : boolean Determine if a particular index exists
isUniqueValue ( string $strTable, string $strField, mixed $varValue, integer $intId = null ) : boolean Check whether a field value in the database is unique
listFields ( string $strTable, boolean $blnNoCache = false ) : array Return all columns of a particular table as array
listTables ( string $strDatabase = null, boolean $blnNoCache = false ) : array Return all tables as array
lockTables ( array $arrTables ) Lock one or more tables
prepare ( string $strQuery ) : Statement Prepare a query and return a Database\Statement object
query ( string $strQuery ) : Result | object Execute a raw query and return a Database\Result object
rollbackTransaction ( ) Rollback a transaction
setDatabase ( string $strDatabase ) Change the current database
tableExists ( string $strTable, string $strDatabase = null, boolean $blnNoCache = false ) : boolean Determine if a particular database table exists
unlockTables ( ) Unlock all tables

Protected Methods

Method Description
__construct ( array $arrConfig ) Establish the database connection

Method Details

__clone() final public method

Prevent cloning of the object (Singleton)
final public __clone ( )

__construct() protected method

Establish the database connection
protected __construct ( array $arrConfig )
$arrConfig array The configuration array

__destruct() public method

Close the database connection
public __destruct ( )

__get() public method

Return an object property
public __get ( string $strKey ) : string | null
$strKey string The property name
return string | null The property value

beginTransaction() public method

Begin a transaction
public beginTransaction ( )

commitTransaction() public method

Commit a transaction
public commitTransaction ( )

execute() public method

Execute a query and return a Database\Result object
public execute ( string $strQuery ) : Result | object
$strQuery string The query string
return Contao\Database\Result | object The Database\Result object

executeCached() public method

Always execute the query and add or replace an existing cache entry
Deprecation: Deprecated since Contao 4.0, to be removed in Contao 5.0. Use Database::execute() instead.
public executeCached ( string $strQuery ) : Result | object
$strQuery string The query string
return Contao\Database\Result | object The Database\Result object

executeUncached() public method

Execute a query and do not cache the result
Deprecation: Deprecated since Contao 4.0, to be removed in Contao 5.0. Use Database::execute() instead.
public executeUncached ( string $strQuery ) : Result | object
$strQuery string The query string
return Contao\Database\Result | object The Database\Result object

fieldExists() public method

Determine if a particular column exists
public fieldExists ( string $strField, string $strTable, boolean $blnNoCache = false ) : boolean
$strField string The field name
$strTable string The table name
$blnNoCache boolean If true, the cache will be bypassed
return boolean True if the field exists

findInSet() public method

Auto-generate a FIND_IN_SET() statement
public findInSet ( string $strKey, mixed $varSet, boolean $blnIsField = false ) : string
$strKey string The field name
$varSet mixed The set to find the key in
$blnIsField boolean If true, the set will not be quoted
return string The FIND_IN_SET() statement

getChildRecords() public method

Return the IDs of all child records of a particular record (see #2475)
Author: Andreas Schempp
public getChildRecords ( mixed $arrParentIds, string $strTable, boolean $blnSorting = false, array $arrReturn = [], string $strWhere = '' ) : array
$arrParentIds mixed An array of parent IDs
$strTable string The table name
$blnSorting boolean True if the table has a sorting field
$arrReturn array The array to be returned
$strWhere string Additional WHERE condition
return array An array of child record IDs

getFieldNames() public method

Return the field names of a particular table as array
public getFieldNames ( string $strTable, boolean $blnNoCache = false ) : array
$strTable string The table name
$blnNoCache boolean If true, the cache will be bypassed
return array An array of field names

getInstance() public static method

Instantiate the Database object (Factory)
public static getInstance ( array $arrCustomConfig = null ) : Database
$arrCustomConfig array A configuration array
return Database The Database object

getNextId() public method

Return the next autoincrement ID of a table
public getNextId ( string $strTable ) : integer
$strTable string The table name
return integer The autoincrement ID

getParentRecords() public method

Return the IDs of all parent records of a particular record
public getParentRecords ( integer $intId, string $strTable ) : array
$intId integer The ID of the record
$strTable string The table name
return array An array of parent record IDs

getSizeOf() public method

Return the table size in bytes
public getSizeOf ( string $strTable ) : integer
$strTable string The table name
return integer The table size in bytes

getUuid() public method

Return a universal unique identifier
public getUuid ( ) : string
return string The UUID string

indexExists() public method

Determine if a particular index exists
public indexExists ( string $strName, string $strTable, boolean $blnNoCache = false ) : boolean
$strName string The index name
$strTable string The table name
$blnNoCache boolean If true, the cache will be bypassed
return boolean True if the index exists

isUniqueValue() public method

Check whether a field value in the database is unique
public isUniqueValue ( string $strTable, string $strField, mixed $varValue, integer $intId = null ) : boolean
$strTable string The table name
$strField string The field name
$varValue mixed The field value
$intId integer The ID of a record to exempt
return boolean True if the field value is unique

listFields() public method

Return all columns of a particular table as array
public listFields ( string $strTable, boolean $blnNoCache = false ) : array
$strTable string The table name
$blnNoCache boolean If true, the cache will be bypassed
return array An array of column names

listTables() public method

Return all tables as array
public listTables ( string $strDatabase = null, boolean $blnNoCache = false ) : array
$strDatabase string The database name
$blnNoCache boolean If true, the cache will be bypassed
return array An array of table names

lockTables() public method

Lock one or more tables
public lockTables ( array $arrTables )
$arrTables array An array of table names to be locked

prepare() public method

Prepare a query and return a Database\Statement object
public prepare ( string $strQuery ) : Statement
$strQuery string The query string
return Contao\Database\Statement The Database\Statement object

query() public method

Execute a raw query and return a Database\Result object
public query ( string $strQuery ) : Result | object
$strQuery string The query string
return Contao\Database\Result | object The Database\Result object

rollbackTransaction() public method

Rollback a transaction
public rollbackTransaction ( )

setDatabase() public method

Change the current database
public setDatabase ( string $strDatabase )
$strDatabase string The name of the target database

tableExists() public method

Determine if a particular database table exists
public tableExists ( string $strTable, string $strDatabase = null, boolean $blnNoCache = false ) : boolean
$strTable string The table name
$strDatabase string The optional database name
$blnNoCache boolean If true, the cache will be bypassed
return boolean True if the table exists

unlockTables() public method

Unlock all tables
public unlockTables ( )

Property Details

$arrCache protected_oe property

Cache
protected array $arrCache
return array

$arrInstances protected_oe static_oe property

Object instances (Singleton)
protected static array $arrInstances
return array

$blnDisableAutocommit protected_oe property

Disable autocommit
protected bool $blnDisableAutocommit
return boolean

$resConnection protected_oe property

Connection ID
protected Connection,Doctrine\DBAL $resConnection
return Doctrine\DBAL\Connection