PRADO Component Framework for PHP 5
  • Home
  • About
  • Testimonials
  • Demos
  • Download
  • Documentation
  • Forum
  • Development
  • Tutorials
  • Class Docs
  • API Manual
  • Wiki

Packages

  • None
  • System
    • Caching
    • Collections
    • Data
      • ActiveRecord
        • Relations
        • Scaffold
          • InputBuilder
      • Commom
        • Sqlite
      • Common
        • Mssql
        • Mysql
        • Oracle
        • Pgsql
        • Sqlite
      • DataGateway
      • SqlMap
        • Configuration
        • Statements
    • Exceptions
    • I18N
    • IO
    • Security
    • Util
    • Web
      • Javascripts
      • Services
      • UI
        • ActiveControls
        • WebControls
    • Xml
  • Wsat
    • pages
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo

Class TDbConnection

TDbConnection class

TDbConnection represents a connection to a database.

TDbConnection works together with TDbCommand, TDbDataReader and TDbTransaction to provide data access to various DBMS in a common set of APIs. They are a thin wrapper of the PDO PHP extension.

To establish a connection, set setActive Active to true after specifying setConnectionString ConnectionString, setUsername Username and setPassword Password.

Since 3.1.2, the connection charset can be set (for MySQL and PostgreSQL databases only) using the setCharset Charset property. The value of this property is database dependant. e.g. for mysql, you can use 'latin1' for cp1252 West European, 'utf8' for unicode, ...

The following example shows how to create a TDbConnection instance and establish the actual connection:

$connection=new TDbConnection($dsn,$username,$password);
$connection->Active=true;

After the DB connection is established, one can execute an SQL statement like the following:

$command=$connection->createCommand($sqlStatement);
$command->execute();   // a non-query SQL statement execution
// or execute an SQL query and fetch the result set
$reader=$command->query();

// each $row is an array representing a row of data
foreach($reader as $row) ...

One can do prepared SQL execution and bind parameters to the prepared SQL:

$command=$connection->createCommand($sqlStatement);
$command->bindParameter($name1,$value1);
$command->bindParameter($name2,$value2);
$command->execute();

To use transaction, do like the following:

$transaction=$connection->beginTransaction();
try
{
   $connection->createCommand($sql1)->execute();
   $connection->createCommand($sql2)->execute();
   //.... other SQL executions
   $transaction->commit();
}
catch(Exception $e)
{
   $transaction->rollBack();
}

TDbConnection provides a set of methods to support setting and querying of certain DBMS attributes, such as getNullConversion NullConversion.

TComponent
Extended by TDbConnection
Package: System\Data
Copyright: Copyright © 2005-2014 PradoSoft
License: http://www.pradosoft.com/license/
Author: Qiang Xue <qiang.xue@gmail.com>
Since: 3.0
Located at Data/TDbConnection.php
Methods summary
public
# __construct( string $dsn = '', string $username = '', string $password = '', string $charset = '' )

Constructor. Note, the DB connection is not established when this connection instance is created. Set setActive Active property to true to establish the connection. Since 3.1.2, you can set the charset for MySql connection

Constructor. Note, the DB connection is not established when this connection instance is created. Set setActive Active property to true to establish the connection. Since 3.1.2, you can set the charset for MySql connection

Parameters

$dsn
string
The Data Source Name, or DSN, contains the information required to connect to the database.
$username
string
The user name for the DSN string.
$password
string
The password for the DSN string.
$charset
string
Charset used for DB Connection (MySql & pgsql only). If not set, will use the default charset of your database server

See

http://www.php.net/manual/en/function.PDO-construct.php

Overrides

TComponent::__construct()
public
# __sleep( )

Close the connection when serializing.

Close the connection when serializing.

Overrides

TComponent::__sleep()
public static array
# getAvailableDrivers( )

Returns

array
list of available PDO drivers

See

http://www.php.net/manual/en/function.PDO-getAvailableDrivers.php
public boolean
# getActive( )

Returns

boolean
whether the DB connection is established
public
# setActive( boolean $value )

Open or close the DB connection.

Open or close the DB connection.

Parameters

$value
boolean
whether to open or close DB connection

Throws

TDbException
if connection fails
protected
# open( )

Opens DB connection if it is currently not

Opens DB connection if it is currently not

Throws

TDbException
if connection fails
protected
# close( )

Closes the currently active DB connection. It does nothing if the connection is already closed.

Closes the currently active DB connection. It does nothing if the connection is already closed.

protected
# setConnectionCharset( )
public string
# getConnectionString( )

Returns

string
The Data Source Name, or DSN, contains the information required to connect to the database.
public
# setConnectionString( string $value )

Parameters

$value
string
The Data Source Name, or DSN, contains the information required to connect to the database.

See

http://www.php.net/manual/en/function.PDO-construct.php
public string
# getUsername( )

Returns

string
the username for establishing DB connection. Defaults to empty string.
public
# setUsername( string $value )

Parameters

$value
string
the username for establishing DB connection
public string
# getPassword( )

Returns

string
the password for establishing DB connection. Defaults to empty string.
public
# setPassword( string $value )

Parameters

$value
string
the password for establishing DB connection
public string
# getCharset( )

Returns

string
the charset used for database connection. Defaults to emtpy string.
public
# setCharset( string $value )

Parameters

$value
string
the charset used for database connection
public PDO
# getPdoInstance( )

Returns

PDO
the PDO instance, null if the connection is not established yet
public TDbCommand
# createCommand( string $sql )

Creates a command for execution.

Creates a command for execution.

Parameters

$sql
string
SQL statement associated with the new command.

Returns

TDbCommand
the DB command

Throws

TDbException
if the connection is not active
public TDbTransaction
# getCurrentTransaction( )

Returns

TDbTransaction
the currently active transaction. Null if no active transaction.
public TDbTransaction
# beginTransaction( )

Starts a transaction.

Starts a transaction.

Returns

TDbTransaction
the transaction initiated

Throws

TDbException
if the connection is not active
public string
# getTransactionClass( )

Returns

string
Transaction class name to be created by calling TDbConnection::beginTransaction(). Defaults to 'System.Data.TDbTransaction'.

Since

3.1.7
public
# setTransactionClass( string $value )

Parameters

$value
string
Transaction class name to be created by calling TDbConnection::beginTransaction().

Since

3.1.7
public string
# getLastInsertID( string $sequenceName = '' )

Returns the ID of the last inserted row or sequence value.

Returns the ID of the last inserted row or sequence value.

Parameters

$sequenceName
string
name of the sequence object (required by some DBMS)

Returns

string
the row ID of the last row inserted, or the last value retrieved from the sequence object

See

http://www.php.net/manual/en/function.PDO-lastInsertId.php
public string
# quoteString( string $str )

Quotes a string for use in a query.

Quotes a string for use in a query.

Parameters

$str
string
string to be quoted

Returns

string
the properly quoted string

See

http://www.php.net/manual/en/function.PDO-quote.php
public string
# quoteTableName( string $name )

Quotes a table name for use in a query.

Quotes a table name for use in a query.

Parameters

$name
string
$name table name

Returns

string
the properly quoted table name
public string
# quoteColumnName( string $name )

Quotes a column name for use in a query.

Quotes a column name for use in a query.

Parameters

$name
string
$name column name

Returns

string
the properly quoted column name
public string
# quoteColumnAlias( string $name )

Quotes a column alias for use in a query.

Quotes a column alias for use in a query.

Parameters

$name
string
$name column name

Returns

string
the properly quoted column alias
public TDbMetaData
# getDbMetaData( )

Returns

TDbMetaData
public TDbColumnCaseMode
# getColumnCase( )

Returns

TDbColumnCaseMode
the case of the column names
public
# setColumnCase( TDbColumnCaseMode $value )

Parameters

$value
TDbColumnCaseMode
the case of the column names
public TDbNullConversionMode
# getNullConversion( )

Returns

TDbNullConversionMode
how the null and empty strings are converted
public
# setNullConversion( TDbNullConversionMode $value )

Parameters

$value
TDbNullConversionMode
how the null and empty strings are converted
public boolean
# getAutoCommit( )

Returns

boolean
whether creating or updating a DB record will be automatically committed. Some DBMS (such as sqlite) may not support this feature.
public
# setAutoCommit( boolean $value )

Parameters

$value
boolean
whether creating or updating a DB record will be automatically committed. Some DBMS (such as sqlite) may not support this feature.
public boolean
# getPersistent( )

Returns

boolean
whether the connection is persistent or not Some DBMS (such as sqlite) may not support this feature.
public
# setPersistent( boolean $value )

Parameters

$value
boolean
whether the connection is persistent or not Some DBMS (such as sqlite) may not support this feature.
public string
# getDriverName( )

Returns

string
name of the DB driver
public string
# getClientVersion( )

Returns

string
the version information of the DB driver
public string
# getConnectionStatus( )

Returns

string
the status of the connection Some DBMS (such as sqlite) may not support this feature.
public boolean
# getPrefetch( )

Returns

boolean
whether the connection performs data prefetching
public string
# getServerInfo( )

Returns

string
the information of DBMS server
public string
# getServerVersion( )

Returns

string
the version information of DBMS server
public integer
# getTimeout( )

Returns

integer
timeout settings for the connection
public mixed
# getAttribute( integer $name )

Obtains a specific DB connection attribute information.

Obtains a specific DB connection attribute information.

Parameters

$name
integer
the attribute to be queried

Returns

mixed
the corresponding attribute information

See

http://www.php.net/manual/en/function.PDO-getAttribute.php
public
# setAttribute( integer $name, mixed $value )

Sets an attribute on the database connection.

Sets an attribute on the database connection.

Parameters

$name
integer
the attribute to be set
$value
mixed
the attribute value

See

http://www.php.net/manual/en/function.PDO-setAttribute.php
Methods inherited from TComponent
__call(), __destruct(), __get(), __isset(), __set(), __unset(), __wakeup(), addParsedObject(), asa(), attachBehavior(), attachBehaviors(), attachClassBehavior(), attachEventHandler(), canGetProperty(), canSetProperty(), clearBehaviors(), createdOnTemplate(), detachBehavior(), detachBehaviors(), detachClassBehavior(), detachEventHandler(), disableBehavior(), disableBehaviors(), enableBehavior(), enableBehaviors(), evaluateExpression(), evaluateStatements(), fxAttachClassBehavior(), fxDetachClassBehavior(), getAutoGlobalListen(), getBehaviorsEnabled(), getClassHierarchy(), getEventHandlers(), getListeningToGlobalEvents(), getSubProperty(), hasEvent(), hasEventHandler(), hasProperty(), isa(), listen(), raiseEvent(), setSubProperty(), unlisten()
Constants summary
string DEFAULT_TRANSACTION_CLASS 'System.Data.TDbTransaction'
#

Since

3.1.7
Constants inherited from TComponent
GLOBAL_RAISE_EVENT_LISTENER
Terms of Service | Contact Us
PRADO v3.2.4 API Manual API documentation generated by ApiGen 2.8.0
Copyright © 2006-2014 by the PRADO Group.
Powered by PRADO