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:

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:

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.

If you find this interesting, checkout the PowerDB generator or browse/fork the code for the generator on Bitbucket. Maybe in the future I will get around to write a simple manual for it.

Leave a Reply

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


This site uses Akismet to reduce spam. Learn how your comment data is processed.