Class: Doctrine_Connection

Source Location: /Doctrine/Connection.php

Class Doctrine_Connection

Class Overview

Implements interfaces:

  • Countable (internal interface)
  • IteratorAggregate (internal interface)

Doctrine_Connection

A wrapper layer on top of PDO / Doctrine_Adapter

Doctrine_Connection is the heart of any Doctrine based application.

  1. Event listeners An easy to use, pluggable eventlistener architecture. Aspects such as logging, query profiling and caching can be easily implemented through the use of these listeners
2. Lazy-connecting Creating an instance of Doctrine_Connection does not connect to database. Connecting to database is only invoked when actually needed (for example when query() is being called)

3. Convenience methods Doctrine_Connection provides many convenience methods such as fetchAll(), fetchOne() etc.

4. Modular structure Higher level functionality such as schema importing, exporting, sequence handling etc. is divided into modules. For a full list of connection modules see Doctrine_Connection::$_modules

Located in /Doctrine/Connection.php [line 56]

Doctrine_Object
   |
   --Doctrine_Configurable
      |
      --Doctrine_Connection
Author(s): API Tags:
Abstract:  

Information Tags:
Version:  $Revision: 2290 $
Link:  www.phpdoctrine.com
Since:  1.0
License:  LGPL

Properties

Methods

[ Top ]
Direct descendents
Child Class Description
Doctrine_Connection_Common standard connection, the parent of pgsql, mysql and sqlite
Doctrine_Connection_Db2 Doctrine_Connection_Db2
Doctrine_Connection_Firebird Doctrine_Connection_Firebird
Doctrine_Connection_Informix Doctrine_Connection_Mysql
Doctrine_Connection_Mssql Doctrine_Connection_Mssql
Doctrine_Connection_Oracle Doctrine_Connection_Oracle

[ Top ]
Inherited Properties, Constants, and Methods
Inherited Properties Inherited Methods Inherited Constants

Inherited From Doctrine_Configurable

Doctrine_Configurable::$attributes
Doctrine_Configurable::$parent
Doctrine_Configurable::$_impl

Inherited From Doctrine_Object

Doctrine_Object::$_null

Inherited From Doctrine_Configurable

Doctrine_Configurable::addListener()
addListener
Doctrine_Configurable::addRecordListener()
addRecordListener
Doctrine_Configurable::getAttribute()
returns the value of an attribute
Doctrine_Configurable::getAttributes()
getAttributes returns all attributes as an array
Doctrine_Configurable::getCacheDriver()
getCacheDriver
Doctrine_Configurable::getImpl()
getImpl returns the implementation for given class
Doctrine_Configurable::getListener()
getListener
Doctrine_Configurable::getParent()
getParent returns the parent of this component
Doctrine_Configurable::getRecordListener()
getListener
Doctrine_Configurable::setAttribute()
setAttribute sets a given attribute
Doctrine_Configurable::setEventListener()
Doctrine_Configurable::setImpl()
setImpl binds given class to given template name
Doctrine_Configurable::setListener()
setListener
Doctrine_Configurable::setParent()
sets a parent for this configurable component the parent must be configurable component itself
Doctrine_Configurable::setRecordListener()
setListener

Inherited From Doctrine_Object

Doctrine_Object::getNullObject()
getNullObject returns the null object associated with this object
Doctrine_Object::initNullObject()
initNullObject initializes the null object

[ Top ]
Property Summary
static array   $availableDrivers  
$dbh   $dbh  
string   $driverName  
boolean   $isConnected  
array   $modules  
mixed   $options  
array   $pendingAttributes  
array   $properties  
array   $serverInfo  
array   $supported  
array   $tables  
mixed   $_count  

[ Top ]
Method Summary
static void   getAvailableDrivers()   returns an array of available PDO drivers
Doctrine_Connection   __construct()   the constructor
boolean   addTable()   addTable adds a Doctrine_Table object into connection registry
integer   beginTransaction()   beginTransaction Start a transaction or set a savepoint.
void   clear()   clear clears all repositories
void   close()   close closes the connection
boolean   commit()   commit Commit the database changes done during a transaction that is in progress or release a savepoint. This function may only be called when auto-committing is disabled, otherwise it will fail.
boolean   connect()   connect connects into database
void   convertBooleans()   convertBooleans some drivers need the boolean values to be converted into integers when using DQL API
integer   count()   returns the count of initialized table objects
Doctrine_Record   create()   create creates a record
void   driverName()   converts given driver name
integer   errorCode()   errorCode Fetch the SQLSTATE associated with the last operation on the database handle
array   errorInfo()   errorInfo Fetch extended error information associated with the last operation on the database handle
void   evictTables()   evictTables evicts all tables
PDOStatement|Doctrine_Adapter_Statement   exec()   exec
PDOStatement|Doctrine_Adapter_Statement   execute()   execute
array   fetchAll()   fetchAll
array   fetchArray()   fetchArray
array   fetchAssoc()   fetchAssoc
array   fetchBoth()   fetchBoth
array   fetchColumn()   fetchColumn
mixed   fetchOne()   fetchOne
array   fetchRow()   fetchRow
void   flush()   flush saves all the records from all tables this operation is isolated using a transaction
mixed   getAttribute()   getAttribute retrieves a database connection attribute
PDO   getDbh()   returns the database handler of which this connection uses
ArrayIterator   getIterator()   returns an iterator that iterators through all initialized table objects
Doctrine_Manager   getManager()   returns the manager that created this connection
string   getName()   getName returns the name of this driver
object Doctrine_Table   getTable()   returns a table object for given component name
array   getTables()   returns an array of all initialized tables
integer   getTransactionLevel()   get the current transaction nesting level
boolean   hasTable()   hasTable whether or not this connection has table $name initialized
void   incrementQueryCount()  
boolean   insert()   Inserts a table row with specified data.
void   lastInsertId()   lastInsertId
void   prepare()   prepare
Doctrine_Collection   query()   query queries the database using Doctrine Query Language returns a collection of Doctrine_Record objects
Doctrine_Record|false   queryOne()   query queries the database using Doctrine Query Language and returns the first record found
mixed   quote()   quote quotes given input parameter
string   quoteIdentifier()   Quote a string so it can be safely used as a table or column name
integer   replace()   Execute a SQL REPLACE query. A REPLACE query is identical to a INSERT query, except that if there is already a row in the table with the same key field values, the REPLACE query just updates its values instead of inserting a new row.
void   rethrowException()   rethrowException
boolean   rollback()   rollback
PDOStatement   select()   queries the database with limit and offset added to the query and returns a PDOStatement object
boolean   setAttribute()   setAttribute sets an attribute
void   setCharset()   Set the charset on the current connection
void   setDateFormat()   Set the date/time format for the current connection
PDOStatement|Doctrine_Adapter_Statement   standaloneQuery()   standaloneQuery
boolean   supports()   supports
Doctrine_Connection_Module   __get()   __get lazy loads given module and returns it
string   __toString()   returns a string representation of this object

[ Top ]
Properties
static array   $availableDrivers = array(
'Mysql',
'Pgsql',
'Oracle',
'Informix',
'Mssql',
'Sqlite',
'Firebird'
)
[line 147]
API Tags:
Access:  private


[ Top ]
$dbh   $dbh [line 61]
API Tags:
Access:  protected


[ Top ]
boolean   $isConnected = false [line 74]
API Tags:
Access:  protected


[ Top ]
array   $modules = array('transaction' => false,
'expression' => false,
'dataDict' => false,
'export' => false,
'import' => false,
'sequence' => false,
'unitOfWork' => false,
'formatter' => false,
'util' => false,
)
[line 115]
API Tags:
See:  Doctrine_Sequence
See:  Doctrine_Connection_UnitOfWork
See:  Doctrine_Formatter
See:  Doctrine_Connection::__get()
See:  Doctrine_Transaction
See:  Doctrine_DataDict
See:  Doctrine_Expression
See:  Doctrine_Export
Access:  private


[ Top ]
mixed   $options = array() [line 143]
API Tags:
Access:  protected


[ Top ]
array   $pendingAttributes = array() [line 86]
API Tags:
Access:  protected


[ Top ]
array   $properties = array('sql_comments' => array(array('start' => '--', 'end' => "\n", 'escape' => false),array('start'=>'/*','end'=>'*/','escape'=>false)),'identifier_quoting'=>array('start'=>'"','end'=>'"','escape'=>'"'),'string_quoting'=>array('start'=>"'",'end'=>"'",'escape'=>false,'escape_pattern'=>false),'wildcards'=>array('%','_'),'varchar_max_length'=>255,) [line 128]
API Tags:
Access:  protected


[ Top ]
array   $serverInfo = array() [line 141]
API Tags:
Access:  protected


[ Top ]
array   $supported = array() [line 80]
API Tags:
Access:  protected


[ Top ]
array   $tables = array() [line 66]
API Tags:
Access:  protected


[ Top ]
mixed   $_count [line 156]
API Tags:
Access:  protected


[ Top ]
Methods
static method getAvailableDrivers  [line 224]

  static void getAvailableDrivers( )

returns an array of available PDO drivers


API Tags:
Access:  public


[ Top ]
Constructor __construct  [line 164]

  Doctrine_Connection __construct( Doctrine_Manager $manager, PDO|Doctrine_Adapter_Interface $adapter, [ $user = null], [ $pass = null]  )

the constructor

Parameters:
Doctrine_Manager   $manager:  the manager object
PDO|Doctrine_Adapter_Interface   $adapter:  database driver
   $user: 
   $pass: 

API Tags:
Access:  public


Redefined in descendants as:

[ Top ]
addTable  [line 945]

  boolean addTable( Doctrine_Table $table  )

addTable adds a Doctrine_Table object into connection registry

Parameters:
$table   $table:  a Doctrine_Table object to be added into registry

API Tags:
Access:  public


[ Top ]
beginTransaction  [line 1086]

  integer beginTransaction( [string $savepoint = null]  )

beginTransaction Start a transaction or set a savepoint.

if trying to set a savepoint and there is no active transaction a new transaction is being started

Listeners: onPreTransactionBegin, onTransactionBegin

Parameters:
string   $savepoint:  name of a savepoint to set

API Tags:
Return:  current transaction nesting level
Access:  public

Information Tags:
Throws:  Doctrine_Transaction_Exception if the transaction fails at database level

[ Top ]
clear  [line 987]

  void clear( )

clear clears all repositories


API Tags:
Access:  public


[ Top ]
close  [line 1011]

  void close( )

close closes the connection


API Tags:
Access:  public


[ Top ]
commit  [line 1103]

  boolean commit( [string $savepoint = null]  )

commit Commit the database changes done during a transaction that is in progress or release a savepoint. This function may only be called when auto-committing is disabled, otherwise it will fail.

Listeners: onPreTransactionCommit, onTransactionCommit

Parameters:
string   $savepoint:  name of a savepoint to release

API Tags:
Return:  false if commit couldn't be performed, true otherwise
Access:  public

Information Tags:
Throws:  Doctrine_Validator_Exception if the transaction fails due to record validations
Throws:  Doctrine_Transaction_Exception if the transaction fails at PDO level

[ Top ]
connect  [line 323]

  boolean connect( )

connect connects into database


API Tags:
Access:  public


Redefined in descendants as:

[ Top ]
convertBooleans  [line 562]

  void convertBooleans( array $item  )

convertBooleans some drivers need the boolean values to be converted into integers when using DQL API

This method takes care of that conversion

Parameters:
array   $item: 

API Tags:
Access:  public


Redefined in descendants as:

[ Top ]
count  [line 934]

  integer count( )

returns the count of initialized table objects


API Tags:
Access:  public


Implementation of:
Countable::count

[ Top ]
create  [line 963]

  Doctrine_Record create( string $name  )

create creates a record

create creates a record

Parameters:
string   $name:  component name

API Tags:
Return:  Doctrine_Record object
Access:  public


[ Top ]
driverName  [line 379]

  void driverName( $name  )

converts given driver name

Parameters:
   $name: 

API Tags:
Access:  public


[ Top ]
errorCode  [line 1039]

  integer errorCode( )

errorCode Fetch the SQLSTATE associated with the last operation on the database handle


API Tags:
Access:  public


[ Top ]
errorInfo  [line 1051]

  array errorInfo( )

errorInfo Fetch extended error information associated with the last operation on the database handle


API Tags:
Access:  public


[ Top ]
evictTables  [line 1000]

  void evictTables( )

evictTables evicts all tables


API Tags:
Access:  public


[ Top ]
exec  [line 816]

  PDOStatement|Doctrine_Adapter_Statement exec( string $query, [ $params = array()]  )

exec

Parameters:
string   $query:  sql query
array   $params:  query parameters

API Tags:
Access:  public


[ Top ]
execute  [line 781]

  PDOStatement|Doctrine_Adapter_Statement execute( string $query, [ $params = array()]  )

execute

Parameters:
string   $query:  sql query
array   $params:  query parameters

API Tags:
Access:  public


[ Top ]
fetchAll  [line 595]

  array fetchAll( string $statement, [ $params = array()]  )

fetchAll

Parameters:
string   $statement:  sql query to be executed
array   $params:  prepared statement params

API Tags:
Access:  public


[ Top ]
fetchArray  [line 629]

  array fetchArray( string $statement, [ $params = array()]  )

fetchArray

Parameters:
string   $statement:  sql query to be executed
array   $params:  prepared statement params

API Tags:
Access:  public


[ Top ]
fetchAssoc  [line 652]

  array fetchAssoc( string $statement, [ $params = array()]  )

fetchAssoc

Parameters:
string   $statement:  sql query to be executed
array   $params:  prepared statement params

API Tags:
Access:  public


[ Top ]
fetchBoth  [line 663]

  array fetchBoth( string $statement, [ $params = array()]  )

fetchBoth

Parameters:
string   $statement:  sql query to be executed
array   $params:  prepared statement params

API Tags:
Access:  public


[ Top ]
fetchColumn  [line 641]

  array fetchColumn( string $statement, [ $params = array()], [int $colnum = 0]  )

fetchColumn

Parameters:
string   $statement:  sql query to be executed
array   $params:  prepared statement params
int   $colnum:  0-indexed column number to retrieve

API Tags:
Access:  public


[ Top ]
fetchOne  [line 607]

  mixed fetchOne( string $statement, [ $params = array()], [int $colnum = 0]  )

fetchOne

Parameters:
string   $statement:  sql query to be executed
array   $params:  prepared statement params
int   $colnum:  0-indexed column number to retrieve

API Tags:
Access:  public


[ Top ]
fetchRow  [line 618]

  array fetchRow( string $statement, [ $params = array()]  )

fetchRow

Parameters:
string   $statement:  sql query to be executed
array   $params:  prepared statement params

API Tags:
Access:  public


[ Top ]
flush  [line 975]

  void flush( )

flush saves all the records from all tables this operation is isolated using a transaction


API Tags:
Access:  public

Information Tags:
Throws:  PDOException if something went wrong at database level

[ Top ]
getAttribute  [line 196]

  mixed getAttribute( integer $attribute  )

getAttribute retrieves a database connection attribute

Parameters:
integer   $attribute: 

API Tags:
Access:  public


Redefinition of:
Doctrine_Configurable::getAttribute()
returns the value of an attribute

[ Top ]
getDbh  [line 311]

  PDO getDbh( )

returns the database handler of which this connection uses


API Tags:
Return:  the database handler
Access:  public


[ Top ]
getIterator  [line 925]

  ArrayIterator getIterator( )

returns an iterator that iterators through all initialized table objects

  1.  foreach ($conn as $index => $table{
  2.       print $table;  // get a string representation of each table object
  3.  }


API Tags:
Return:  SPL ArrayIterator object
Access:  public


Implementation of:
IteratorAggregate::getIterator

[ Top ]
getManager  [line 302]

  Doctrine_Manager getManager( )

returns the manager that created this connection


API Tags:
Access:  public


[ Top ]
getName  [line 255]

  string getName( )

getName returns the name of this driver


API Tags:
Return:  the name of this driver
Access:  public


[ Top ]
getTable  [line 886]

  object Doctrine_Table getTable( string $name, [ $allowExport = true]  )

returns a table object for given component name

Parameters:
string   $name:  component name
   $allowExport: 

API Tags:
Access:  public


[ Top ]
getTables  [line 909]

  array getTables( )

returns an array of all initialized tables


API Tags:
Access:  public


[ Top ]
getTransactionLevel  [line 1029]

  integer getTransactionLevel( )

get the current transaction nesting level


API Tags:
Access:  public


[ Top ]
hasTable  [line 876]

  boolean hasTable( mixed $name  )

hasTable whether or not this connection has table $name initialized

Parameters:
mixed   $name: 

API Tags:
Access:  public


[ Top ]
incrementQueryCount  [line 370]

  void incrementQueryCount( )


API Tags:
Access:  public


[ Top ]
insert  [line 467]

  boolean insert( string $table, [ $values = array()]  )

Inserts a table row with specified data.

Parameters:
string   $table:  The table to insert data into.
array   $values:  An associateve array containing column-value pairs.

API Tags:
Access:  public


[ Top ]
lastInsertId  [line 1069]

  void lastInsertId( [string $table = null], [string $field = null]  )

lastInsertId

Returns the ID of the last inserted row, or the last value from a sequence object, depending on the underlying driver.

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 fields or sequences.

Parameters:
string   $table:  name of the table into which a new row was inserted
string   $field:  name of the field into which a new row was inserted

API Tags:
Access:  public


[ Top ]
prepare  [line 694]

  void prepare( string $statement  )

prepare

Parameters:
string   $statement: 

API Tags:
Access:  public


[ Top ]
query  [line 683]

  Doctrine_Collection query( string $query, [ $params = array()]  )

query queries the database using Doctrine Query Language returns a collection of Doctrine_Record objects

  1.  $users $conn->query('SELECT u.* FROM User u');
  2.  
  3.  $users $conn->query('SELECT u.* FROM User u WHERE u.name LIKE ?'array('someone'));

Parameters:
string   $query:  DQL query
array   $params:  query parameters

API Tags:
Return:  Collection of Doctrine_Record objects
See:  Doctrine_Query
Access:  public


[ Top ]
queryOne  [line 736]

  Doctrine_Record|false queryOne( string $query, [ $params = array()]  )

query queries the database using Doctrine Query Language and returns the first record found

  1.  $user $conn->queryOne('SELECT u.* FROM User u WHERE u.id = ?'array(1));
  2.  
  3.  $user $conn->queryOne('SELECT u.* FROM User u WHERE u.name LIKE ? AND u.password = ?',
  4.          array('someone''password')
  5.          );

Parameters:
string   $query:  DQL query
array   $params:  query parameters

API Tags:
Return:  Doctrine_Record object on success, boolean false on failure
See:  Doctrine_Query
Access:  public


[ Top ]
quote  [line 574]

  mixed quote( mixed $input, [string $type = null]  )

quote quotes given input parameter

Parameters:
mixed   $input:  parameter to be quoted
string   $type: 

API Tags:
Access:  public


[ Top ]
quoteIdentifier  [line 541]

  string quoteIdentifier( string $str, [bool $checkOption = true]  )

Quote a string so it can be safely used as a table or column name

Delimiting style depends on which database driver is being used.

NOTE: just because you CAN use delimited identifiers doesn't mean you SHOULD use them. In general, they end up causing way more problems than they solve.

Portability is broken by using the following characters inside delimited identifiers:

  • backtick (`) -- due to MySQL
  • double quote (") -- due to Oracle
  • brackets ([ or ]) -- due to Access
Delimited identifiers are known to generally work correctly under the following drivers:
  • mssql
  • mysql
  • mysqli
  • oci8
  • pgsql
  • sqlite
InterBase doesn't seem to be able to use delimited identifiers via PHP 4. They work fine under PHP 5.

Parameters:
string   $str:  identifier name to be quoted
bool   $checkOption:  check the 'quote_identifier' option

API Tags:
Return:  quoted identifier string
Access:  public


Redefined in descendants as:

[ Top ]
replace  [line 428]

  integer replace( string $table, $fields, $keys  )

Execute a SQL REPLACE query. A REPLACE query is identical to a INSERT query, except that if there is already a row in the table with the same key field values, the REPLACE query just updates its values instead of inserting a new row.

The REPLACE type of query does not make part of the SQL standards. Since practically only MySQL and SQLIte implement it natively, this type of query isemulated through this method for other DBMS using standard types of queries inside a transaction to assure the atomicity of the operation.

Parameters:
array   $keys: 

an array containing all key fields (primary key fields or unique index fields) for this table

the uniqueness of a row will be determined according to the provided key fields

this method will fail if no key fields are specified

string   $table:  name of the table on which the REPLACE query will be executed.
array   $fields: 

an associative array that describes the fields and the values that will be inserted or updated in the specified table. The indexes of the array are the names of all the fields of the table.

The values of the array are values to be assigned to the specified field.


API Tags:
Return:  number of rows affected
Access:  public

Information Tags:
Throws:  Doctrine_Connection_Exception if this driver doesn't support replace
Throws:  PDOException if something fails at PDO level
Throws:  Doctrine_Connection_Exception if some of the key values was null
Throws:  Doctrine_Connection_Exception if there were no key fields

Redefined in descendants as:
  • Doctrine_Connection_Mysql::replace() : Execute a SQL REPLACE query. A REPLACE query is identical to a INSERT query, except that if there is already a row in the table with the same key field values, the REPLACE query just updates its values instead of inserting a new row.

[ Top ]
rethrowException  [line 849]

  void rethrowException( $e, $invoker  )

rethrowException

Parameters:
Exception   $e: 
   $invoker: 

API Tags:
Access:  public

Information Tags:
Throws:  Doctrine_Connection_Exception

[ Top ]
rollback  [line 1121]

  boolean rollback( [string $savepoint = null]  )

rollback

Cancel any database changes done during a transaction or since a specific savepoint that is in progress. This function may only be called when auto-committing is disabled, otherwise it will fail. Therefore, a new transaction is implicitly started after canceling the pending changes.

this method can be listened with onPreTransactionRollback and onTransactionRollback eventlistener methods

Parameters:
string   $savepoint:  name of a savepoint to rollback to

API Tags:
Return:  false if rollback couldn't be performed, true otherwise
Access:  public

Information Tags:
Throws:  Doctrine_Transaction_Exception if the rollback operation fails at database level

[ Top ]
select  [line 755]

  PDOStatement select( string $query, [integer $limit = 0], [integer $offset = 0]  )

queries the database with limit and offset added to the query and returns a PDOStatement object

Parameters:
string   $query: 
integer   $limit: 
integer   $offset: 

API Tags:
Access:  public


[ Top ]
setAttribute  [line 236]

  boolean setAttribute( integer $attribute, mixed $value  )

setAttribute sets an attribute

Parameters:
integer   $attribute: 
mixed   $value: 

API Tags:
Access:  public


Redefinition of:
Doctrine_Configurable::setAttribute()
setAttribute sets a given attribute

[ Top ]
setCharset  [line 505]

  void setCharset( string $charset  )

Set the charset on the current connection

Parameters:
string   $charset:  charset

API Tags:
Access:  public


Redefined in descendants as:

[ Top ]
setDateFormat  [line 585]

  void setDateFormat( [string $format = null]  )

Set the date/time format for the current connection

Parameters:
string   $format:  time format

API Tags:
Access:  public


Redefined in descendants as:

[ Top ]
standaloneQuery  [line 770]

  PDOStatement|Doctrine_Adapter_Statement standaloneQuery( string $query, [array $params = array()]  )

standaloneQuery

Parameters:
string   $query:  sql query
array   $params:  query parameters

API Tags:
Access:  public


[ Top ]
supports  [line 388]

  boolean supports( string $feature  )

supports

Parameters:
string   $feature:  the name of the feature

API Tags:
Return:  whether or not this drivers supports given feature
Access:  public


[ Top ]
__get  [line 272]

  Doctrine_Connection_Module __get( string $name  )

__get lazy loads given module and returns it

Parameters:
string   $name:  the name of the module to get

API Tags:
Return:  connection module
See:  Doctrine_Connection::$modules all availible modules
See:  Doctrine_DataDict
See:  Doctrine_Transaction
See:  Doctrine_Export
See:  Doctrine_Expression
Access:  public

Information Tags:
Throws:  Doctrine_Connection_Exception if trying to get an unknown module

[ Top ]
__toString  [line 1130]

  string __toString( )

returns a string representation of this object


API Tags:
Access:  public


[ Top ]