BerlinDB provides an ORM-like interface for custom database tables in WordPress.
Use it when custom post types, taxonomies, or post meta are no longer the right
storage model for your data, but you still want a WordPress-native developer
experience: wpdb compatibility, schema objects, query builders, row objects,
caching hooks, and table upgrade routines.
- PHP 8.1 or newer
- WordPress
- Composer
composer require berlindb/coreA typical integration defines four small classes:
- a
Schemathat describes columns and indexes - a
Tablethat creates and upgrades the database table - a
Rowthat shapes returned records - a
Querythat reads and writes records
<?php
namespace Acme\Plugin\Database;
use BerlinDB\Database\Kern\Schema;
class WidgetSchema extends Schema {
public $columns = array(
array(
'name' => 'id',
'type' => 'bigint',
'length' => '20',
'unsigned' => true,
'extra' => 'auto_increment',
'default' => false,
'cache_key' => true,
'sortable' => true,
),
array(
'name' => 'name',
'type' => 'varchar',
'length' => '200',
'default' => '',
'searchable' => true,
'sortable' => true,
),
array(
'name' => 'status',
'type' => 'varchar',
'length' => '20',
'default' => 'active',
'cache_key' => true,
'in' => true,
'not_in' => true,
),
array(
'name' => 'date_created',
'type' => 'datetime',
'default' => '',
'created' => true,
'sortable' => true,
),
);
public $indexes = array(
array(
'type' => 'primary',
'columns' => array( 'id' ),
),
array(
'name' => 'status',
'type' => 'key',
'columns' => array( 'status' ),
),
);
}<?php
namespace Acme\Plugin\Database;
use BerlinDB\Database\Kern\Table;
class WidgetTable extends Table {
protected $schema = WidgetSchema::class;
protected $name = 'acme_widgets';
protected $version = '202605280';
}Create or upgrade the table during your plugin's install or upgrade routine:
( new WidgetTable() )->install();<?php
namespace Acme\Plugin\Database;
use BerlinDB\Database\Kern\Row;
class Widget extends Row {
public $id = 0;
public $name = '';
public $status = 'active';
public $date_created = '';
}<?php
namespace Acme\Plugin\Database;
use BerlinDB\Database\Kern\Query;
class WidgetQuery extends Query {
protected $prefix = 'acme';
protected $table_name = 'widgets';
protected $table_alias = 'w';
protected $table_schema = WidgetSchema::class;
protected $item_name = 'widget';
protected $item_name_plural = 'widgets';
protected $item_shape = Widget::class;
protected $cache_group = 'acme-widgets';
}$query = new WidgetQuery();
$widget_id = $query->add_item(
array(
'name' => 'Example',
'status' => 'active',
)
);
$widget = $query->get_item( $widget_id );
$active_widgets = $query->query(
array(
'status__in' => array( 'active', 'pending' ),
'orderby' => 'date_created',
'order' => 'DESC',
'number' => 20,
)
);
$query->update_item(
$widget_id,
array(
'status' => 'archived',
)
);
$query->delete_item( $widget_id );The project wiki contains deeper documentation for the current object model, including adapters, interfaces, traits, parsers, operators, schemas, tables, and queries.
Install dependencies:
composer installRun the default test suite:
bin/run-tests.sh -- --group defaultRun the suite against a specific PHP and WordPress version:
bin/run-tests.sh -p 8.1 -w 6.7 -- --group defaultRun static analysis and coding standards:
vendor/bin/phpstan analyse --memory-limit=1G
vendor/bin/phpcsSee CONTRIBUTING.md for the full local workflow.
BerlinDB is named for WordCamp Europe 2019 in Berlin, Germany, where it was originally exhibited and announced as an unnamed utility being used by the Sandhills Development engineering team.
Peter Wilson recommended naming it "Berlin" to commemorate everyone in attendance for its unveiling. Thanks, Peter.
BerlinDB is open-source software licensed under the MIT license.