Last year I wrote about developing  custom tables in WordPress using my library IronBound-DB.

Custom Tables in WordPress

Today I’m releasing the beta of version 2.0 of that library which brings a lot of changes to make the library a lot easier to work with. It’s modeled after Laravel’s Eloquent – an Active Record pattern.

Simply put, it allows you to do things like this.

$book = Book::create();
$book->price = 24.99;

$author = Author::get( 3 );
$author->books->add( $book );


This is just the tip of the iceberg. v2 supports:

  • Relations like HasOne, HasMany, and ManyToMany. Relations are also cached.
  • Foreign key constraints. These are implemented in PHP so WordPress actions are properly fired and non-InnoDB table types can be used.
  • Eager-loading to prevent the N+1 problem. Nested eager-loading is supported as well.
  • A FluentQuery class to make retrieving data much simpler and more intuitive.
  • Integration with WordPress content types. Posts, Users, Comments, and Terms can be exposed as model attributes. They can be modified and then saved directly through the Model.
  • Built-in metadata support.
  • Trash support. ( PHP 5.4+ only )
  • Query scopes to globally modify a result set.
  • Fire events (actions) when models are saved, updated, or deleted.
  • Basic extensions framework. This is how Trashing, Meta, and Foreign Key Constraints are implemented.
  • Auto-expansion of column types. DATETIME converts to a \DateTime instance and TIME converts to \DateInterval. Numeric columns are also castes to their respective types as well.


Upgrading to v2 is very simple. The only code change you should have to make is to change Table::get_columns() to return Column objects instead of sprintf return types like %s.

What’s Coming

WP-API support. You’ll be able to install an additional package which will provide support for Models to be automatically exposed in the API. This will probably also facilitate the devlopment of an OOP wrapper for the WP-API.

I aim to build it in such a way that it will be automatically compatible with the WP-API Backbone library. This will allow for the devlopment of  administrative and front end UIs very simple.

Learn More

IronBound-DB is available on GitHub under the MIT license and can be installed via composer. Issues and Pull-requests are welcome.