MIKESTOWE.COM

Just another php, religion, and politics weblog

DB::DBAL


DB::DBAL has been moved to http://dbal.mikestowe.com.


DB::DBAL is a lightweight Database Abstraction Layer that is designed to allow for multiple database types and connections simultaneously. DB::DBAL provides ease of use of use through query chaining, security via automatic where clause escaping, as well as code reusability through the ability to add universal events for table queries (pre/post fetch, insert, update, or save).

An example DB::DBAL MySQL setup and call might look like this:

PHP
1
2
3
4
5
6
7
8
9
10
11
// Require the DB Class
require_once('classes/DB.php');
// Setup a Connection (saving it is optional and allows you to go back and forth)
DB::MySQL()->connect('localhost','username','password')->saveConnectionAs('default');
// Select Database
DB::MySQL()->db('exampleDatabase');
$result = DB::MySQL()->table('MyTable')->select()->where('column = ?', $data)->fetchOne();
echo $result->name; //name column

DB::DBAL also maps the data to an object and allows for quick data manipulation and saving through the save() method:

PHP
1
2
$result->name = 'New Name';
$result->save();

Along with mapping and saving the new data, DB::DBAL remembers old data and allows for easy access of data that has been changed through the oldData() and changedData() methods:

PHP
1
2
3
4
5
var_dump($result->oldData()); // returns an array of Old Data
var_dump($result->changedData()); // returns an array of columns with old and new values
var_dump($result->changedData('name')); // returns the new value of the column name
var_dump($result->changedData('name', 'old'); // returns the old value of the column name

To retrieve the count of selected rows, affected rows, or insert id, DB:DBAL provides the following methods:

PHP
1
2
3
$result->count(); // number of rows in query
$result->affectedRows(); // number of rows effected by save/ insert/ update
$result->insertId(); // insert ID if a new record

DB::DBAL also allows for array based data insertion or record updating:

PHP
1
2
3
4
5
6
7
8
// Update All Records
DB::MySQL()->table('MyTable')->update(array('name' => 'New Name'))->save();
// With Where Clause
DB::MySQL()->table('MyTable')->update(array('name' => 'New Name'))->where('name = ?', 'Old Name')->save();
// Insert Record
DB::MySQL()->table('MyTable')->insert(array('name' => 'New Name'))->save();

DB::DBAL also contains a Models folder in the classes/DB/ folder. Models allow you to create table classes (based on database type, database name, and table name) that will have event methods run on certain actions. For example, you can create a postSave() method in the table class to perform a specific set of actions everytime data is updated or inserted into that specific table. This may mean formatting the data a specific way or porting data to other tables.

For more on models, read the ReadMe.txt file in the Models/ folder.


Installing DB::DBAL

DB::DBAL can be run on any server utilizing PHP 5.3+

Bugs

  • No known errors


Awards (Yay)
- listed in the order received
We’re working on this… lol

Usage
DB::DABL was released under the GPL version 2 license.

Download

Note – DB::DBAL is in pre-alpha release and is currently being developed. Please feel free to download, test, and commit changes to help improve this application. Future implementations will include MySQLi, other databases, and more caching options. Oh yeah, it will also include documentation – feel free to commit documentation/ doc blox as well!

https://github.com/mikestowe/

Facebook Twitter Linkedin Digg Reddit Stumbleupon Tumblr Posterous Email Snailmail
Share

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>