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);
Afficher le fichier Open project: contao/core-bundle Class Usage Examples

Protected Properties

Свойство Type Description
$arrCache array Cache
$arrInstances array Object instances (Singleton)
$blnDisableAutocommit boolean Disable autocommit
$resConnection Doctrine\DBAL\Connection Connection ID

Méthodes publiques

Méthode 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

Méthodes protégées

Méthode Description
__construct ( array $arrConfig ) Establish the database connection

Method Details

__clone() final public méthode

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

__construct() protected méthode

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

__destruct() public méthode

Close the database connection
public __destruct ( )

__get() public méthode

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

beginTransaction() public méthode

Begin a transaction
public beginTransaction ( )

commitTransaction() public méthode

Commit a transaction
public commitTransaction ( )

execute() public méthode

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

executeCached() public méthode

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
Résultat Contao\Database\Result | object The Database\Result object

executeUncached() public méthode

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
Résultat Contao\Database\Result | object The Database\Result object

fieldExists() public méthode

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
Résultat boolean True if the field exists

findInSet() public méthode

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
Résultat string The FIND_IN_SET() statement

getChildRecords() public méthode

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
Résultat array An array of child record IDs

getFieldNames() public méthode

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
Résultat array An array of field names

getInstance() public static méthode

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

getNextId() public méthode

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

getParentRecords() public méthode

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
Résultat array An array of parent record IDs

getSizeOf() public méthode

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

getUuid() public méthode

Return a universal unique identifier
public getUuid ( ) : string
Résultat string The UUID string

indexExists() public méthode

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
Résultat boolean True if the index exists

isUniqueValue() public méthode

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
Résultat boolean True if the field value is unique

listFields() public méthode

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
Résultat array An array of column names

listTables() public méthode

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
Résultat array An array of table names

lockTables() public méthode

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

prepare() public méthode

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

query() public méthode

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

rollbackTransaction() public méthode

Rollback a transaction
public rollbackTransaction ( )

setDatabase() public méthode

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

tableExists() public méthode

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
Résultat boolean True if the table exists

unlockTables() public méthode

Unlock all tables
public unlockTables ( )

Property Details

$arrCache protected_oe property

Cache
protected array $arrCache
Résultat array

$arrInstances protected_oe static_oe property

Object instances (Singleton)
protected static array $arrInstances
Résultat array

$blnDisableAutocommit protected_oe property

Disable autocommit
protected bool $blnDisableAutocommit
Résultat boolean

$resConnection protected_oe property

Connection ID
protected Connection,Doctrine\DBAL $resConnection
Résultat Doctrine\DBAL\Connection