Single Table Inheritance for CakePHP 3 ORM.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Build Status Coverage Total Downloads License

Single Table Inheritance for CakePHP 3 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:1.0.x-dev

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

bin/cake plugin load Muffin/Sti

or by manually adding statement shown below to bootstrap.php:



<?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, Use Muffin and licensed under The MIT License.