class Illuminate / Database / Connection
You are viewing an older version of the documentation. For the latest, please visit master documentation.

Connection

Implements

Uses

Properties

protected

$pdo PDO |Closure

The active PDO connection.

Default: null
protected

$readPdo PDO |Closure

The active PDO connection used for reads.

Default: null
protected

$database string

The name of the connected database.

Default: null
protected

$readWriteType string|null

The type of the connection.

Default: null
protected

$tablePrefix string

The table prefix for the connection.

Default: ''
protected

$config array

The database connection configuration options.

Default: []
protected

$reconnector callable

The reconnector instance for the connection.

Default: null
protected

$queryGrammar Grammar

The query grammar implementation.

Default: null
protected

$schemaGrammar Grammar

The schema grammar implementation.

Default: null
protected

$postProcessor Processor

The query post processor implementation.

Default: null
protected

$events Dispatcher

The event dispatcher instance.

Default: null
protected

$fetchMode int

The default fetch mode of the connection.

Default: FETCH_OBJ
protected

$transactions int

The number of active transactions.

protected

$transactionsManager DatabaseTransactionsManager

The transaction manager instance.

Default: null
protected

$recordsModified bool

Indicates if changes have been made to the database.

Default: false
protected

$readOnWriteConnection bool

Indicates if the connection should use the "write" PDO connection.

Default: false
protected

$queryLog array

All of the queries run against the connection.

Default: []
protected

$loggingQueries bool

Indicates whether queries are being logged.

Default: false
protected

$pretending bool

Indicates if the connection is in a "dry run".

Default: false
protected

$beforeExecutingCallbacks array

All of the callbacks that should be invoked before a query is executed.

Default: []
protected

$doctrineConnection Doctrine\...\Connection

The instance of Doctrine connection.

Default: null
protected

$doctrineTypeMappings array

Type mappings that should be registered with new Doctrine connections.

Default: []
protected static

$resolvers array

The connection resolvers.

Default: []

Methods

public

__construct ( $pdo , $database , $tablePrefix , array $config ) : void

Create a new database connection instance.

Parameters

  • $pdo PDO |Closure
  • $database string
  • $tablePrefix string
  • $config array
public

useDefaultQueryGrammar ( ) : void

Set the query grammar to the default implementation.

protected

getDefaultQueryGrammar ( ) : Grammar

Get the default query grammar instance.

public

useDefaultSchemaGrammar ( ) : void

Set the schema grammar to the default implementation.

protected

getDefaultSchemaGrammar ( ) : Grammar

Get the default schema grammar instance.

public

useDefaultPostProcessor ( ) : void

Set the query post processor to the default implementation.

protected

getDefaultPostProcessor ( ) : Processor

Get the default post processor instance.

public

getSchemaBuilder ( ) : Builder

Get a schema builder instance for the connection.

public

table ( $table , $as ) : Builder

Begin a fluent query against a database table.

Parameters

public

query ( ) : Builder

Get a new query builder instance.

public

selectOne ( $query , $bindings , $useReadPdo ) : mixed

Run a select statement and return a single result.

Parameters

  • $query string
  • $bindings array
  • $useReadPdo bool
public

selectFromWriteConnection ( $query , $bindings ) : array

Run a select statement against the database.

Parameters

  • $query string
  • $bindings array
public

select ( $query , $bindings , $useReadPdo ) : array

Run a select statement against the database.

Parameters

  • $query string
  • $bindings array
  • $useReadPdo bool
public

cursor ( $query , $bindings , $useReadPdo ) : Generator

Run a select statement against the database and returns a generator.

Parameters

  • $query string
  • $bindings array
  • $useReadPdo bool
protected

prepared ( PDOStatement $statement ) : PDOStatement

Configure the PDO prepared statement.

Parameters

protected

getPdoForSelect ( $useReadPdo ) : PDO

Get the PDO connection to use for a select query.

Parameters

  • $useReadPdo bool
public

insert ( $query , $bindings ) : bool

Run an insert statement against the database.

Parameters

  • $query string
  • $bindings array
public

update ( $query , $bindings ) : int

Run an update statement against the database.

Parameters

  • $query string
  • $bindings array
public

delete ( $query , $bindings ) : int

Run a delete statement against the database.

Parameters

  • $query string
  • $bindings array
public

statement ( $query , $bindings ) : bool

Execute an SQL statement and return the boolean result.

Parameters

  • $query string
  • $bindings array
public

affectingStatement ( $query , $bindings ) : int

Run an SQL statement and get the number of rows affected.

Parameters

  • $query string
  • $bindings array
public

unprepared ( $query ) : bool

Run a raw, unprepared query against the PDO connection.

Parameters

  • $query string
public

pretend ( Closure $callback ) : array

Execute the given callback in "dry run" mode.

Parameters

protected

withFreshQueryLog ( $callback ) : array

Execute the given callback in "dry run" mode.

Parameters

public

bindValues ( $statement , $bindings ) : void

Bind values to their parameters in the given statement.

Parameters

public

prepareBindings ( array $bindings ) : array

Prepare the query bindings for execution.

Parameters

  • $bindings array
protected

run ( $query , $bindings , Closure $callback ) : mixed

Run a SQL statement and log its execution context.

Parameters

  • $query string
  • $bindings array
  • $callback Closure
protected

runQueryCallback ( $query , $bindings , Closure $callback ) : mixed

Run a SQL statement.

Parameters

  • $query string
  • $bindings array
  • $callback Closure
public

logQuery ( $query , $bindings , $time ) : void

Log a query in the connection's query log.

Parameters

  • $query string
  • $bindings array
  • $time float|null
protected

getElapsedTime ( $start ) : float

Get the elapsed time since a given starting point.

Parameters

  • $start int
protected

handleQueryException ( QueryException $e , $query , $bindings , Closure $callback ) : mixed

Handle a query exception.

Parameters

protected

tryAgainIfCausedByLostConnection ( QueryException $e , $query , $bindings , Closure $callback ) : mixed

Handle a query exception that occurred during query execution.

Parameters

public

reconnect ( ) : void

Reconnect to the database.

protected

reconnectIfMissingConnection ( ) : void

Reconnect to the database if a PDO connection is missing.

public

disconnect ( ) : void

Disconnect from the underlying PDO connection.

public

beforeExecuting ( Closure $callback ) : $this

Register a hook to be run just before a database query is executed.

Parameters

public

listen ( Closure $callback ) : void

Register a database query listener with the connection.

Parameters

protected

fireConnectionEvent ( $event ) : array|null

Fire an event for this connection.

Parameters

  • $event string
protected

event ( $event ) : void

Fire the given event if possible.

Parameters

  • $event mixed
public

raw ( $value ) : Expression

Get a new raw query expression.

Parameters

  • $value mixed
public

hasModifiedRecords ( ) : bool

Determine if the database connection has modified any database records.

public

recordsHaveBeenModified ( $value ) : void

Indicate if any records have been modified.

Parameters

  • $value bool
public

setRecordModificationState ( bool $value ) : $this

Set the record modification state.

Parameters

  • $value bool
public

forgetRecordModificationState ( ) : void

Reset the record modification state.

public

useWriteConnectionWhenReading ( $value ) : $this

Indicate that the connection should use the write PDO connection for reads.

Parameters

  • $value bool
public

isDoctrineAvailable ( ) : bool

Is Doctrine available?

public

getDoctrineColumn ( $table , $column ) : Doctrine\...\Column

Get a Doctrine Schema Column instance.

Parameters

  • $table string
  • $column string
public

getDoctrineSchemaManager ( ) : Doctrine\...\AbstractSchemaManager

Get the Doctrine DBAL schema manager for the connection.

public

getDoctrineConnection ( ) : Doctrine\...\Connection

Get the Doctrine DBAL database connection instance.

public

registerDoctrineType ( string $class , string $name , string $type ) : void

Register a custom Doctrine mapping type.

Parameters

  • $class string
  • $name string
  • $type string
public

getPdo ( ) : PDO

Get the current PDO connection.

public

getRawPdo ( ) : PDO |Closure |null

Get the current PDO connection parameter without executing any reconnect logic.

public

getReadPdo ( ) : PDO

Get the current PDO connection used for reading.

public

getRawReadPdo ( ) : PDO |Closure |null

Get the current read PDO connection parameter without executing any reconnect logic.

public

setPdo ( $pdo ) : $this

Set the PDO connection.

Parameters

public

setReadPdo ( $pdo ) : $this

Set the PDO connection used for reading.

Parameters

public

setReconnector ( callable $reconnector ) : $this

Set the reconnect instance on the connection.

Parameters

  • $reconnector callable
public

getName ( ) : string|null

Get the database connection name.

public

getNameWithReadWriteType ( ) : string|null

Get the database connection full name.

public

getConfig ( $option ) : mixed

Get an option from the configuration options.

Parameters

  • $option string|null
public

getDriverName ( ) : string

Get the PDO driver name.

public

getQueryGrammar ( ) : Grammar

Get the query grammar used by the connection.

public

setQueryGrammar ( Grammar $grammar ) : $this

Set the query grammar used by the connection.

Parameters

public

getSchemaGrammar ( ) : Grammar

Get the schema grammar used by the connection.

public

setSchemaGrammar ( Grammar $grammar ) : $this

Set the schema grammar used by the connection.

Parameters

public

getPostProcessor ( ) : Processor

Get the query post processor used by the connection.

public

setPostProcessor ( Processor $processor ) : $this

Set the query post processor used by the connection.

Parameters

public

getEventDispatcher ( ) : Dispatcher

Get the event dispatcher used by the connection.

public

setEventDispatcher ( Dispatcher $events ) : $this

Set the event dispatcher instance on the connection.

Parameters

public

unsetEventDispatcher ( ) : void

Unset the event dispatcher for this connection.

public

setTransactionManager ( $manager ) : $this

Set the transaction manager instance on the connection.

Parameters

public

unsetTransactionManager ( ) : void

Unset the transaction manager for this connection.

public

pretending ( ) : bool

Determine if the connection is in a "dry run".

public

getQueryLog ( ) : array

Get the connection query log.

public

flushQueryLog ( ) : void

Clear the query log.

public

enableQueryLog ( ) : void

Enable the query log on the connection.

public

disableQueryLog ( ) : void

Disable the query log on the connection.

public

logging ( ) : bool

Determine whether we're logging queries.

public

getDatabaseName ( ) : string

Get the name of the connected database.

public

setDatabaseName ( $database ) : $this

Set the name of the connected database.

Parameters

  • $database string
public

setReadWriteType ( $readWriteType ) : $this

Set the read / write type of the connection.

Parameters

  • $readWriteType string|null
public

getTablePrefix ( ) : string

Get the table prefix for the connection.

public

setTablePrefix ( $prefix ) : $this

Set the table prefix in use by the connection.

Parameters

  • $prefix string
public

withTablePrefix ( Grammar $grammar ) : Grammar

Set the table prefix and return the grammar.

Parameters

public static

resolverFor ( $driver , Closure $callback ) : void

Register a connection resolver.

Parameters

public static

getResolver ( $driver ) : mixed

Get the connection resolver for the given driver.

Parameters

  • $driver string