db-to-php: The new “intelligent” PHP database framework


What is exactly db-to-php

Before I answer that question, let’s look shortly at the different types of database frameworks.

The database frameworks are divided in two main categories:

  • ORM (object relational mapping), ex: Doctrine
  • Active Record, ex: Eloquent by Laravel

db-to-php was created with the intention of being in neither of them, but just as a tool which can be incrementally integrated to represent database tables in code, giving the user the full flexibility on decisions such as how to connect to the database, how to write, save etc.

This changed with the new feature branch, in which I decided to start offering ORM functionalities. There is an Entity Manager that will be offered out of the box, and entities will also track the state of the changes.

That means that you will be able to save, delete, update entities very easily like in any other ORM, and also updates will be very fast due to updating only the changed fields. (Dirty fields are tracked in entities).

So what does db-to-php does exactly and why should you use it ?

db-to-php does:

  1. Generate entities for each table specified.
  2. Generate entity factories for each table specified.
  3. A basic Entity Manager (started implementing the ORM pattern in the feature branch). Entities track changes, and can be saved and updated. During updates, only the changed fields are updated.
  4. Use the generated factories for unit tests and seeding dummy data.

When should you use it?

You can use it in any project as long as it is well defined. This tool is a must for projects that need high quality, proper testing, and reduce the level of possible errors.

Type hinting, strongly formalizing database tables into classes are the strongest points against other frameworks like for example Eloquent.

In Eloquent nothing would prevent a developer from trying to access a non-existing column in a database. The fields are accessed dynamically, and no one knows what fields are available.

In db-to-php,  the editor of choice would suggest properly the available fields, as they are methods or properties (depending on your settings) of the generated class.

Why I created db-to-php

While there are many PHP database frameworks out there, I found none of them to have the following functionalities:

  • automatically generates the corresponding entity classes based on a table schema
  • determine the field types correctly
  • generate factories, which can create random data correctly depending on the type for each table column, and objects of the generated entities

And then I decided to create db-to-php.

If you come from a .NET background, db-to-php does what LINQ TO SQL does, aside from the active record functionality.

Example of entity creation

Let’s see a simple example of entity generation. Suppose we have the table with the following MySQL definition:

After running the  command

the following entity is generated at the location specified in the config file:

As can be seen above, the field types are detected, type hints are used, and annotations are filled properly. The settings can be changed in the configuration files according to your generation preferences.

A full manual is located at the Github page of the library, at db-to-php.

Example of entity factory creation

Let’s see another example, in which we generate an entity factory.

The table schema is the same as in the example of entity generation.

After running

the following entity factory is generated:

The generated data respects strictly the field types of the database table schema.

Not only the types, but also the lengths (int4, int8, etc) and special types are detected (year, date, etc).

The common use cases of entity factories are:

  1. Used for simplifying unit tests. Instead of mocking any method of your entities, you can auto generate an entity via the static make method, and override only the necessary fields.
  2. Can be used for generating seed data and populating a database with articles, posts etc.

For a full documentation on how to install and use the framework, check its Github page: db-to-php.

Thank you for reading. I would like you to leave your comments below with suggestions, critics and any insight you might have.

And of course, I would be glad if you would want to contribute.