Class \Prado\Data\TDbConnection
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 \Prado\Data\setActive to true after specifying \Prado\Data\setConnectionString, \Prado\Data\setUsername and \Prado\Data\setPassword.
Since 3.1.2, the connection charset can be set (for MySQL and PostgreSQL databases only) using the \Prado\Data\setCharset 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 \Prado\Data\getNullConversion.
Class hierarchy
Author: Qiang Xue <qiang.xue@gmail.com>Since: 3.0
public
|
__construct([string $dsn = '' ][, string $username = '' ][, string $password = '' ][, string $charset = '' ]) : mixed
Constructor.
Note, the DB connection is not established when this connection instance is created. Set \Prado\Data\setActive property to true to establish the connection. Since 3.1.2, you can set the charset for MySql connection |
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
static
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
getLastInsertID([string $sequenceName = '' ]) : string
Returns the ID of the last inserted row or sequence value.
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
protected
|
close() : mixed
Closes the currently active DB connection.
It does nothing if the connection is already closed. |
protected
|
|
protected
|
public
mixed
|
DEFAULT_TRANSACTION_CLASS
|
\Prado\Data\TDbTransaction::class
|
\Prado\TComponent::GLOBAL_RAISE_EVENT_LISTENER |