Exposing my own php object generator – PowerDB
Once upon a time I wrote my own database ORM. It is heavily influenced by the PHP Object Generator. Which was a very easy to use tool for generating database objects in PHP.
Sadly POG had its flaws:
- Low reliability under pressure, a lot of weird things exhausting the db connections
- Lousy code (wildlife PHP code)
- Only produced direct queries without security IE. INSERT into books VALUES (‘Title’, ‘2023’, ’24’);
- Only MySQL support
- No development
So the main reason why I wrote PowerDB was because that I needed support for both PostGreSQL and MySQL for different setups. I was also tired of trying to hack the miserable PHP code that POG written in. It resulted in a PHP library that can do the following:
- Creating database relations such as CHILD, PARENT and SIBLING
- All queries are made through PDO statements to prevent injection and other errors.
- Fast (if used in the right way)
- Designed to produce maintainable code
- Support for transactions via PDO
In PowerDB you generate base classes via a web interface. The web interface also contains a package where you can try out your models and generate the database tables. It actually enables you to produce some complex data models. I wrote a project using 40 tables.
The whole idea is that PowerDB generates base classes which are meant to be derived from, so you can put your custom code in the derived classes. To make it easier to re-generate models that needs an extra field in the database.
Here is a brief coding example how it supposed to be used:
// Create an author
$William = new Author();
$William->name = "William Subwoofer";
$William->birthYear = 1957;
// Add a book and assign to the Author
$Book1 = new Book();
$Book1->title = "I wish I was a robber";
$Book1->year = 2012;
$Book1->pages = 243;
// Add another book
$Book2 = new Book();
$Book2->title = "I'm leaving the city";
$Book2->year = 1998;
$Book2->pages = 23;
// Add a library
$Library = new Library();
$Library->address = "23 Long Road st."
$Library->city = "Gotham";
// Add the author to the Library
// And save recursively
The code is pretty self explanatory. PowerDB gives a clean way of working with database entities. The Author object has child objects books. Each book has a parent called Author, which is set automatically. The library object has authors as child objects. Since all the objects are related to the Library object I only need to call save on the “master” object and it will create an author, two books and a library.
It also supports sibling relationship which actually means that an entity (for instance a book) can point to many different entities. For instance a book can point to related books.
To query the data later on is fairly simple:
$Library = Library::fetchAll(array(array('city', '=', 'Gotham')));
$AuthorList = $Library->getAuthorList(array(array('name', '=', 'William Subwoofer')));
$Author = array_pop($AuthorList);
// Prints out the title of the first book in the array.
The code is just an idea on how the developer will use the different objects to access data. (Of course you should include checking when writing proper code.)
I’ve been using PowerDB in a lot of small projects, and I believe it’s a great tool for small to medium PHP projects. There are some things that would be nice to have in the library as support for composer distribution, namespaces etc, support for more databases.