PHPackages                             ironbound/db - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. ironbound/db

ActiveLibrary[Database &amp; ORM](/categories/database)

ironbound/db
============

Provides models and custom query objects for custom database tables in WordPress.

v2.1.0(9y ago)338196[2 issues](https://github.com/iron-bound-designs/IronBound-DB/issues)[2 PRs](https://github.com/iron-bound-designs/IronBound-DB/pulls)1MITPHPPHP &gt;=5.3.0CI failing

Since Oct 5Pushed 6y ago6 watchersCompare

[ Source](https://github.com/iron-bound-designs/IronBound-DB)[ Packagist](https://packagist.org/packages/ironbound/db)[ RSS](/packages/ironbound-db/feed)WikiDiscussions master Synced yesterday

READMEChangelog (7)Dependencies (5)Versions (10)Used By (1)

IronBound DB
============

[](#ironbound-db)

[![Build Status](https://camo.githubusercontent.com/70e6cee0e357e489fbb33fd02ad4eee8ad85b5393cabb6e7462c9e038746a755/68747470733a2f2f7472617669732d63692e6f72672f69726f6e2d626f756e642d64657369676e732f49726f6e426f756e642d44422e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/iron-bound-designs/IronBound-DB) [![codecov](https://camo.githubusercontent.com/81a36344ddea4516ceb45ab80115a880dd1d8c0981d9847894ad8327d0a38c22/68747470733a2f2f636f6465636f762e696f2f67682f69726f6e2d626f756e642d64657369676e732f49726f6e426f756e642d44422f67726170682f62616467652e737667)](https://codecov.io/gh/iron-bound-designs/IronBound-DB)

IronBound DB is a straightforward, but powerful, ORM for WordPress. It facilitates the creation of custom database tables, and bridges the gap between WordPress functionality and IronBound Models.

Installs with composer.

```
composer require ironbound/db v2.0.0

```

Basic Usage
-----------

[](#basic-usage)

### Relationships

[](#relationships)

#### One to Many

[](#one-to-many)

```
$author = Author::create( array( 'name' => 'John Doe' ) );
$author->books->add( new Book( array( 'title' => "John's Book" ) ) );
$author->save();

// Eager load One-to-Many Books relationship to prevent N+1 problem
$authors = Author::with( 'books' )->results();

foreach ( $authors as $author ) {
	foreach ( $author->books as $book ) {
		$book->price += 5.00;
	}
}

$authors->save();
```

#### Many to Many

[](#many-to-many)

```
$book1 = Book::create( array( 'title' => 'Book 1' ) );
$book2 = Book::create( array( 'title' => 'Book 2' ) );
$book3 = Book::create( array( 'title' => 'Book 3' ) );

$bronx		= Library::create( array( 'name' => 'Bronx Library' ) );
$manhattan  = Library::create( array( 'name' => 'Manhattan Library' ) );

$manhattan->books->add( $book2 );
$manhattan->books->add( $book3 );
$manhattan->save();

$bronx->books->add( $book1 );
$bronx->books->add( $book2 );
$bronx->save();

$manhattan->books->removeElement( $book3 );
$manhattan->books->add( $book1 );
$manhattan->save();
```

### Interacting with WordPress Models

[](#interacting-with-wordpress-models)

Model's can have any of the four WordPress objects (Posts, Users, Comments, Terms) as attributes. They will be saved or created whenever the Model is saved.

```
$author = Author::get( 1 );
$author->user->display_name = 'John Doe'; // This is a WP_User object
$author->picture->post_title = 'John Doe'; // This is a WP_Post object
$author->save(); // The User and Post were automatically saved

// You can have WP objects inserted too

$author = new Author(
	'picture' => new WP_Post( (object) array(
		'post_type'  => 'attachment',
		'post_title' => 'Jane Doe'
	) );
);
$author->save();
```

### Meta Support

[](#meta-support)

```
Author::create( array(
 	'name' => 'John Doe'
 ) )->update_meta( 'favorite_color', 'blue' );

Author::create( array(
 	'name' => 'Jane Doe'
 ) )->update_meta( 'favorite_color', 'green' );

$authors = Author::query()->where_meta( array(
	array(
		'key' 	=> 'favorite_color',
		'value' => 'blue'
	)
) )->results();
```

### Events

[](#events)

When used with [WPEvents](https://github.com/iron-bound-designs/IronBound-WPEvents), IronBound DB will fire events (actions) when various state changes occur.

```
Book::created( function( GenericEvent $event ) {

	$book = $event->get_subject();
	// do custom processing...
} );

Book::saving( function( GenericEvent $event ) {

	$book = $event->get_subject();

	if ( $book->price === 0 ) {
		throw new UnexpectedValueException( 'Books are not allowed to be free!' );
	}
} );
```

### Foreign Key Constrains

[](#foreign-key-constrains)

If the constraint behavior is set to `cascade`, this will delete both the Author and the Book. `deleted` events will be fired for each model. Constraints require `WPEvents` be configured.

```
$author = Author::create( array( 'name' => 'John Doe' ) );
$book 	= Book::create( array( 'title' => "John's Book", 'author' => $author ) );

$author->delete();
```

Defining Models
---------------

[](#defining-models)

```
class Author extends \IronBound\DB\Model\ModelWithMeta {

	public function get_pk() {
		return $this->id;
	}

	protected function _books_relation() {

		$relation = new HasMany( 'author', get_class( new Book() ), $this, 'books' );
		$relation->keep_synced();

		return $relation;
	}

	protected static function get_table() {
		return static::$_db_manager->get( 'authors' );
	}

	public static function get_meta_table() {
    		return static::$_db_manager->get( 'authors-meta' );
    }
}
```

Defining Tables
---------------

[](#defining-tables)

Tables are defined in PHP. Each model is represented by a table. Tables are generally defined by a PHP class.

### Basic Model Tables

[](#basic-model-tables)

```
class Authors extends \IronBound\DB\Table\BaseTable {

	public function get_table_name( \wpdb $wpdb ) {
		return "{$wpdb->prefix}authors";
	}

	public function get_slug() {
		return 'authors';
	}
	public function get_columns() {
		return array(
			'id'         => new \IronBound\DB\Table\Column\IntegerBased( 'BIGINT', 'id', array( 'unsigned', 'auto_increment' ), array( 20 ) ),
			'name'       => new \IronBound\DB\Table\Column\StringBased( 'VARCHAR', 'name', array(), array( 60 ) ),
			'birth_date' => new \IronBound\DB\Table\Column\DateTime( 'birth_date' ),
			'bio'        => new \IronBound\DB\Table\Column\StringBased( 'LONGTEXT', 'bio' ),
			'picture'    => new \IronBound\DB\Table\Column\ForeignPost( 'picture', new \IronBound\DB\Saver\PostSaver() ),
			'user'    	 => new \IronBound\DB\Table\Column\ForeignUser( 'user', new \IronBound\DB\Saver\UserSaver() )
		);
	}

	public function get_column_defaults() {
		return array(
			'id'         => 0,
			'name'       => '',
			'birth_date' => '',
			'bio'        => '',
			'picture'    => 0,
			'user'		 => 0
		);
	}

	public function get_primary_key() {
		return 'id';
	}

	public function get_version() {
		return 1;
	}
}

\IronBound\DB\Manager::register( new Authors() );
\IronBound\DB\Manager::register( new BaseMetaTable( new Authors() ) );
\IronBound\DB\Manager::maybe_install_table( new Authors() );
\IronBound\DB\Manager::maybe_install_table( new BaseMetaTable( new Authors() ) );
```

### Foreign Key Constrains

[](#foreign-key-constrains-1)

```
class Authors extends \IronBound\DB\Table\BaseTable implements \IronBound\DB\Table\ForeignKey\DeleteConstrained {

	// ... other methods

	public function get_delete_constraints() {
		return array(
			'user' 	  => self::RESTRICT    // Exception thrown when deleting a User if an author referencing it exists,
			'picture' => self::SET_DEFAULT // The column will be updated to its default value when its referenced post is deleted
		);
	}
}
```

[Learn more about it](https://timothybjacobs.com/2016/07/27/ironbound-db-v2/).

###  Health Score

34

—

LowBetter than 75% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 91.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~90 days

Recently: every ~132 days

Total

7

Last Release

3384d ago

Major Versions

v1.1 → v2.0-beta2016-07-27

### Community

Maintainers

![](https://www.gravatar.com/avatar/c348d8c0ee55f04ab227c1084486ec12037713b2919f91f16c774625a33391b9?d=identicon)[TimothyBJacobs](/maintainers/TimothyBJacobs)

---

Top Contributors

[![TimothyBJacobs](https://avatars.githubusercontent.com/u/3460448?v=4)](https://github.com/TimothyBJacobs "TimothyBJacobs (215 commits)")[![shazahm1](https://avatars.githubusercontent.com/u/1185008?v=4)](https://github.com/shazahm1 "shazahm1 (21 commits)")

---

Tags

ormwordpress

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ironbound-db/health.svg)

```
[![Health](https://phpackages.com/badges/ironbound-db/health.svg)](https://phpackages.com/packages/ironbound-db)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k300.5M7.5k](/packages/doctrine-orm)[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.5k5.9M737](/packages/sylius-sylius)[api-platform/core

Build a fully-featured hypermedia or GraphQL API in minutes!

2.6k51.2M339](/packages/api-platform-core)[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.4M203](/packages/sulu-sulu)[getgrav/grav

Modern, Crazy Fast, Ridiculously Easy and Amazingly Powerful Flat-File CMS

15.6k86.4k1](/packages/getgrav-grav)[tempest/framework

The PHP framework that gets out of your way.

2.2k34.4k15](/packages/tempest-framework)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
