Connections and Databases

Connect to databases

Use DatabaseManager::connect() to connect to databases.

Connect to Transactd Sever

Set alias DB for DatabaseManager.

class_alias('Transactd\DatabaseManager', 'DB');

Connect to different Master server and Slave server

Transactd PHP ORM supports to Use master and slave differently. It uses the master for write operations, and uses the slave for read operations.

$masterUri = 'tdap://yousername@your_master_host/your_database?&pwd=xxxx';
$slaveUri = 'tdap://yousername@your_slave_host/your_database?&pwd=xxxx';
DB::connect($masterUri, $slaveUri);

Connect to the server which behave as both of master and slave

If you did not specify the slave, ORM uses the master for both of read/write operations. If you did not specify the master, ORM will be readonly.

$masterUri = 'tdap://yousername@your_master_host/your_database?&pwd=xxxx';
DB::connect($masterUri, null);

Connect to multiple servers

You can manage multiple connections with $name parameter.

There are two connections, to the master and to the slave. Manage these connections with one name. If you did not specify the name, default will be used.

Connect to the server which behave as both of master and slave, with name Internal

$masterUri = 'tdap://yousername@your_master_host/your_database?&pwd=xxxx';
DB::connect($masterUri, null, 'Internal');

See also URI format.

Specify Connection

Get Database object of Internal connection.

$db = DB::connection('Internal')->master();

Return default connection if the name was not specified.

$db = DB::slave();

Get QueryExecuter for the customers table with Internal connection.

$qe = DB::connection('Internal')->queryExecuter('customers');

Connection pool and table cache

Transactd PHP ORM pools connections by default. It operates at high speed without negotiating the TCP connnection every request.

You can also cache table handles. Then table schema information will be cached local, and the handle will be kept without closing.

Set to use table handle cache

Set enable or disable with DatabaseManager::$tableCash on initialization of the application.

DB::$tableCash = true;

If enable it once, it keeps the table handles until calling DatabaseManager::reset() or the end of the process.

If the cache is disabled, table will be closed each time the web application completes processing for one request.

Close table handle and reset schema cache

If the application changes table schema and the table is required to be recreated, it will fail because its schema is locked with table cache. In this case, use DatabaseManager::reset() to release table handles and connection.

DB::reset();