DB::DBAL

The World's Most Powerful Light-Weight DBAL

Advanced Use Case

Here is an example of what you can do with DB::DBAL Connections:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
DB::MySQL()->connect('host', 'username', 'password')->saveConnectionAs('conn1');
DB::MySQL()->connect('host2', 'username', 'password')->saveConnectionAs('conn2');
DB::Firebird()->connect('database_path')->saveConnectionAs('conn1');
DB::Firebird()->connect('database2_path')->saveConnectionAs('conn2');
// Note in MySQL and Firebird we are currently connected to "conn2."
DB::MySQL()->db('database_in_conn2');
// Now we are connected to database_in_conn2
DB::MySQL()->table('myTable')->select()->fetchAll(); // get all records
DB::MySQL()->table('myTable')->select()->fetch(10,20); // limit 20 with offset of 10
// We already specified our database in Firebird's connection method
DB::Firebird()->table('myTable')->select()->fetchOne(); // get one record
//Now switch back to conn1:
DB::MySQL()->useConnection('conn1');
DB::MySQL()->db('database_in_conn1');
DB::MySQL()->table('myTable')->find(1); // primaryId = 1
DB::MySQL()->table('myTable')->find(2); // primaryId = 2
DB::MySQL()->table('myTable')->find(3); // primaryId = 3
// Switch Databases in conn1
DB::MySQL()->db('database2_in_conn1');
DB::MySQL()->table('myTable')->find(1); // running on database2
// Switch back to Connection 2
DB::MySQL()->useConnection('conn2');
DB::MySQL()->db('database_in_conn_2'); // currently you need to re-select the database after switching connections
DB::MySQL()->table('myTable')->findOneByName('John Doe');
?>

As you can see, DB::DBAL Connection’s allow for extreme versatility and flexibility when working with more than one database, or even more than one database type.