PHPackages                             spmartin/cakephp-table-inheritance - 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. [Framework](/categories/framework)
4. /
5. spmartin/cakephp-table-inheritance

ActiveCakephp-plugin[Framework](/categories/framework)

spmartin/cakephp-table-inheritance
==================================

CakePHP STI &amp; CTI pattern plugin

02PHP

Since Mar 22Pushed 2y agoCompare

[ Source](https://github.com/spmartin/cakephp-table-inheritance)[ Packagist](https://packagist.org/packages/spmartin/cakephp-table-inheritance)[ RSS](/packages/spmartin-cakephp-table-inheritance/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (2)Used By (0)

CakePHP Table Inheritance plugin
================================

[](#cakephp-table-inheritance-plugin)

[![Latest Stable Version](https://camo.githubusercontent.com/e973ade50268e398f045e8a05475123af36c2346dd0d8295feb3e7aa14a0c5f3/68747470733a2f2f706f7365722e707567782e6f72672f73706d617274696e2f63616b657068702d7461626c652d696e6865726974616e63652f762f737461626c65)](https://packagist.org/packages/spmartin/cakephp-table-inheritance)[![Total Downloads](https://camo.githubusercontent.com/1d33445ddb0a6a84b4fecee0727f73666a51cb99f25e149a91b1d582bbc4469d/68747470733a2f2f706f7365722e707567782e6f72672f73706d617274696e2f63616b657068702d7461626c652d696e6865726974616e63652f646f776e6c6f616473)](https://packagist.org/packages/spmartin/cakephp-table-inheritance)[![Software License](https://camo.githubusercontent.com/074b89bca64d3edc93a1db6c7e3b1636b874540ba91d66367c0e5e354c56d0ea/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)](LICENSE)[![Build Status](https://camo.githubusercontent.com/d57362b43029789e665a01a7b51627e63c460dae0ebba3f08b7c19e18059363f/68747470733a2f2f7472617669732d63692e6f72672f73706d617274696e2f63616b657068702d7461626c652d696e6865726974616e63652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/spmartin/cakephp-table-inheritance)[![codecov](https://camo.githubusercontent.com/b2fde84c0b8929efe3bba16636dd860ec8597e39cf4f0e39d2585b4fc01a7e07/68747470733a2f2f636f6465636f762e696f2f67682f73706d617274696e2f63616b657068702d7461626c652d696e6865726974616e63652f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/spmartin/cakephp-table-inheritance/branch/master)

This plugin implements [Single Table Inheritance](https://en.wikipedia.org/wiki/Single_Table_Inheritance) (and hopefully will implement Class Table Inheritance in the future) patterns for CakePHP ORM.

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

[](#installation)

CakePHP 5.x

Using composer:

```
composer require spmartin/cakephp-table-inheritance

```

For CakePHP 4.x use version 0.5 of the plugin

```
composer require robotusers/cakephp-table-inheritance:^0.5

```

StiBehavior
-----------

[](#stibehavior)

For now only STI is supported. Just add a behavior to your tables:

```
//in ClientsTable:
public function initialize(array $config)
{
    $this->addBehavior('Robotusers/TableInheritance.Sti', [
        'table' => 'users',
        'discriminator' => 'client'
    ]);
}

//alternative config in AdministratorsTable:
public function initialize(array $config)
{
    $this->table('users');
    $this->addBehavior('Robotusers/TableInheritance.Sti');
    $this->setDiscriminator('admin');
}
```

Now both the `ClientsTable` and `AdministratorsTable` will share `users` db table. A table has to have a `discriminator` field which will be used to determine which model's record is stored in a row.

### Multiple discriminators

[](#multiple-discriminators)

You can also configure a list of allowed discriminators. It's useful for example when working with the files. For example:

```
//in ImagesTable:
public function initialize(array $config)
{
    $this->addBehavior('Robotusers/TableInheritance.Sti', [
        'table' => 'files',
        'discriminatorField' => 'mime',
        'acceptedDiscriminators' => [
            'image/jpeg',
            'image/gif',
            'image/png',
            'image/tiff'
        ]
    ]);
}

//or using wildcards:

public function initialize(array $config)
{
    $this->addBehavior('Robotusers/TableInheritance.Sti', [
        'table' => 'files',
        'discriminatorField' => 'mime',
        'acceptedDiscriminators' => [
            'image/*'
        ]
    ]);
}
```

An `ImagesTable` will share `files` db table and match only specified mime types.

You can also add accepted discriminators on runtime:

```
$table->addAcceptedDiscriminator('image/bmp');
```

### Configuration

[](#configuration)

`StiBehavior` supports following options:

- `discriminatorField` - db table field used to discriminate models, 'discriminator' by default
- `discriminator` - default discriminator value, `$table->alias()` by default
- `table` - db table to share, use this option or `$table->table()` method.
- `checkRules` - `true` by default. Allows to enable/disable build-in rule check for a discriminator value.
- `acceptedDiscriminators` - a list of accepted discriminators.

StiParentBehavior
-----------------

[](#stiparentbehavior)

This plugin also allows to configure parent Table in order to create and hydrate entities based on child tables.

```
//in UsersTable:
public function initialize(array $config)
{
    $this->addBehavior('Robotusers/TableInheritance.StiParent', [
        'tableMap' => [
            'Administrators' => [
                'admin',
                'administrator'
            ],
            'Clients' => 'client'
        ]
    ]);
}
```

`tableMap` option accepts an array mapping table registry aliases to discriminator field values.

You can also map discriminator values to specified table objects using `discriminatorMap` option:

```
//in UsersTable:
public function initialize(array $config)
{
    $this->addBehavior('Robotusers/TableInheritance.StiParent', [
        'discriminatorMap' => [
            'admin' => $this->tableLocator()->get('Administrators'),
            'client' => $this->tableLocator()->get('Clients')
        ]
    ]);
}
```

This behavior also provides `newStiEntity()` method which will proxy `newEntity()` to one of the configured tables based on a discriminator value.

```
$data = [
    'name' => 'super-admin',
    'discriminator' => 'admin'
];

$admin = $this->Users->newStiEntity($data); //will call AdministratorsTable::newEntity() and return an Administrator entity instance.
```

Afterwards you can get a STI table using `stiTable()` method and handle entity using its source `Table` object.

```
$table = $this->Users->stiTable($admin);
$table->save($admin); //it will save an entity using AdministratorsTable
```

You can also directly detect STI table from data array:

```
$data = [
    'name' => 'super-admin',
    'discriminator' => 'admin'
];

$table = $this->Users->stiTable($data);
$admin = $table->newEntity($data);
$table->save($admin);
```

### Original code written by Robert Pustułka

[](#original-code-written-by-robert-pustułka-robertpustulkagmailcom)

###

[](#httpsgithubcomrobotuserscakephp-table-inheritance)

###  Health Score

13

—

LowBetter than 1% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity2

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity21

Early-stage or recently created project

 Bus Factor1

Top contributor holds 90.8% 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/93954039be9fdf129e869c7a779e2839c8b92fd683b8d0ed2c42ddc1661e7916?d=identicon)[spmartin](/maintainers/spmartin)

---

Top Contributors

[![robertpustulka](https://avatars.githubusercontent.com/u/7437773?v=4)](https://github.com/robertpustulka "robertpustulka (59 commits)")[![themrwilliams](https://avatars.githubusercontent.com/u/132327?v=4)](https://github.com/themrwilliams "themrwilliams (4 commits)")[![josephshanak](https://avatars.githubusercontent.com/u/5422469?v=4)](https://github.com/josephshanak "josephshanak (1 commits)")[![spmartin](https://avatars.githubusercontent.com/u/556252?v=4)](https://github.com/spmartin "spmartin (1 commits)")

### Embed Badge

![Health badge](/badges/spmartin-cakephp-table-inheritance/health.svg)

```
[![Health](https://phpackages.com/badges/spmartin-cakephp-table-inheritance/health.svg)](https://phpackages.com/packages/spmartin-cakephp-table-inheritance)
```

###  Alternatives

[laravel/passport

Laravel Passport provides OAuth2 server support to Laravel.

3.4k85.0M532](/packages/laravel-passport)[nolimits4web/swiper

Most modern mobile touch slider and framework with hardware accelerated transitions

41.8k177.2k1](/packages/nolimits4web-swiper)[laravel/dusk

Laravel Dusk provides simple end-to-end testing and browser automation.

1.9k36.7M259](/packages/laravel-dusk)[laravel/prompts

Add beautiful and user-friendly forms to your command-line applications.

712181.8M596](/packages/laravel-prompts)[cakephp/chronos

A simple API extension for DateTime.

1.4k47.7M121](/packages/cakephp-chronos)[laravel/pail

Easily delve into your Laravel application's log files directly from the command line.

91545.3M590](/packages/laravel-pail)

PHPackages © 2026

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