PHPackages                             stellarwp/schema - 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. stellarwp/schema

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

stellarwp/schema
================

A library for simplifying the creation and updates of custom tables within WordPress.

3.2.0(4mo ago)29519.6k↑25.8%2[13 issues](https://github.com/stellarwp/schema/issues)2GPL-2.0PHPCI passing

Since Aug 17Pushed 4mo ago19 watchersCompare

[ Source](https://github.com/stellarwp/schema)[ Packagist](https://packagist.org/packages/stellarwp/schema)[ RSS](/packages/stellarwp-schema/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (20)Versions (23)Used By (2)

Schema Builder
==============

[](#schema-builder)

[![CI](https://github.com/stellarwp/schema/workflows/CI/badge.svg)](https://github.com/stellarwp/schema/actions?query=branch%3Amain) [![Static Analysis](https://github.com/stellarwp/schema/actions/workflows/static-analysis.yml/badge.svg)](https://github.com/stellarwp/schema/actions/workflows/static-analysis.yml)

A library for simplifying the creation, update, and field modification of custom tables within WordPress.

Installation
------------

[](#installation)

It's recommended that you install Schema as a project dependency via [Composer](https://getcomposer.org/):

```
composer require stellarwp/schema
```

> We *actually* recommend that this library gets included in your project using [Strauss](https://github.com/BrianHenryIE/strauss).
>
> Luckily, adding Strauss to your `composer.json` is only slightly more complicated than adding a typical dependency, so checkout our [strauss docs](https://github.com/stellarwp/global-docs/blob/main/docs/strauss-setup.md).

Usage prerequisites
-------------------

[](#usage-prerequisites)

To actually *use* the schema library, you must have two additional libraries in your project:

1. A Dependency Injection Container (DI Container) that is compatible with [di52](https://github.com/lucatume/di52) *(We recommend using di52.)*.
2. The [stellarwp/db](https://github.com/stellarwp/db) library.

In order to keep this library as light as possible, those dependencies are not included in the library itself. To avoid version compatibility issues, those libraries are *also* not included as Composer dependencies. Instead, you must include them in your project. We recommend including them via composer [using Strauss](https://github.com/stellarwp/global-docs/blob/main/docs/strauss-setup.md), just like you have done with this library.

Important note on ALL examples
------------------------------

[](#important-note-on-all-examples)

All examples within the documentation for this project will be assuming that you are using [Strauss](#strauss) to prefix the namespaces provided by this library.

The examples will be using:

- `Boom\Shakalaka\` as the namespace prefix, though will sometimes be referenced as `PREFIX\` for the purpose of brevity in the docs.
- `BOOM_SHAKALAKA_` as the constant prefix.

What's new in 3.0.0
-------------------

[](#whats-new-in-300)

Version 3.0.0 introduces major new features and breaking changes:

- **Type-safe Column Definitions**: Define table columns using strongly-typed classes (`Integer_Column`, `String_Column`, `Float_Column`, etc.) instead of raw SQL
- **Index Management**: Create and manage indexes with dedicated classes (`Primary_Key`, `Unique_Key`, `Classic_Index`, `Fulltext_Index`)
- **Schema History**: Track and manage schema changes over time with the `get_schema_history()` method
- **Enhanced Query Methods**: Access built-in CRUD operations through the `Custom_Table_Query_Methods` trait
- **Improved Type Safety**: Automatic type casting and validation for PHP/MySQL data transformation

**Note**: Version 3.0.0 is NOT backwards compatible with 2.x. See the [migration guide](docs/migrating-from-v2-to-v3.md) for upgrading from v2 to v3.

Getting started
---------------

[](#getting-started)

For a full understanding of what is available in this library and how to use it, definitely read through the full [documentation](#documentation). But for folks that want to get rolling with the basics quickly, try out the following.

### Initializing the library

[](#initializing-the-library)

```
use Boom\Shakalaka\StellarWP\Schema\Config;

// You'll need a Dependency Injection Container that is compatible with https://github.com/lucatume/di52.
use Boom\Shakalaka\lucatume\DI52\Container;

// You'll need to use the StellarWP\DB library for database operations.
use Boom\Shakalaka\StellarWP\DB\DB;

$container = new Boom\Shakalaka\lucatume\DI52\Container();

Config::set_container( $container );
Config::set_db( DB::class );
```

### Creating a table (v3.0+)

[](#creating-a-table-v30)

Let's say you want a new custom table called `sandwiches` (with the default WP prefix, it'd be `wp_sandwiches`). You'll need a class file for the table. For the sake of this example, we'll be assuming this class is going into a `Tables/` directory and is reachable via the `Boom\Shakalaka\Tables` namespace.

**Version 3.0** introduces a new, type-safe way to define tables using Column and Index classes:

```
