Single Table Inheritance for CakePHP ORM

Installs: 572

Dependents: 0

Suggesters: 0

Security: 0

Stars: 7

Watchers: 6

Forks: 5


2.0.0-beta 2020-01-16 13:41 UTC

This package is auto-updated.

Last update: 2021-11-25 14:27:34 UTC


Build Status Coverage Total Downloads License

Single Table Inheritance for CakePHP ORM.

[...] a way to emulate object-oriented inheritance in a relational database. When mapping from a database table to an object in an object-oriented language, a field in the database identifies what class in the hierarchy the object belongs to.

(source: Wikipedia)


Using Composer:

composer require muffin/sti

You then need to load the plugin. You can use the console command:

bin/cake plugin load Muffin/Sti


<?php // src/Model/Table/CooksTable.php
namespace App\Model\Table;

use Cake\ORM\Table;

class CooksTable extends Table
    public function initialize($config)
        $this->addBehavior('Muffin/Sti.Sti', [
            'typeMap' => [
                'chef' => 'App\Model\Entity\Chef',
                'baker' => 'App\Model\Entity\Baker',
                'assistant_chef' => 'App\Model\Entity\AssistantChef',

        // Optionally, set the default type. If none is defined, the
        // first one (i.e. `chef`) will be used.

Then create a class for every type of entity:

  • Chef
  • Baker
  • AssistantChef

The entity that was previously defined to be the 'default' one will need to use the StiAwareTrait:

<?php // src/Model/Entity/AssistantChef.php
namespace App\Model\Entity;

use Cake\ORM\Entity;
use Muffin\Sti\Model\Entity\StiAwareTrait;

class AssistantChef extends Entity
    use StiAwareTrait;

Optionally, you can create classes for your tables that extend the parent table to encapsulate business logic:

<?php // src/Model/Table/ChefsTable.php
namespace App\Model\Table;

class ChefsTable extends CooksTable
  // ...

I said optionally, because if the only thing you need is some extra validation rules, you could define those on the parent table. For example, to add custom rules to chefs:

// src/Model/Table/CooksTable.php
public function validationChefs(Validator $validator)
    // ...
    return $validator;

New entities

The behavior will automatically add helper methods for creating entities of different types (i.e. newChef()). There are different ways of creating new entities, all are valid and depending on the cases, you might need one or the other:

// using the parent table

// or, using the parent table again
$cooks->newEntity(['type' => 'chef', ...]);

// or, using the child table


For the above examples to work using (*chef), you need to add a custom rule to the Inflector:

Cake\Utility\Inflector::rules('plural', ['/chef$/i' => '\1Chefs']);

Patches & Features

  • Fork
  • Mod, fix
  • Test - this is important, so it's not unintentionally broken
  • Commit - do not mess with license, todo, version, etc. (if you do change any, bump them into commits of their own that I can ignore when I pull)
  • Pull request - bonus point for topic branches

To ensure your PRs are considered for upstream, you MUST follow the CakePHP coding standards.

Bugs & Feedback


Copyright (c) 2015-Present, Use Muffin and licensed under The MIT License.