muffin/sti

Single Table Inheritance for CakePHP ORM

Installs: 772

Dependents: 0

Suggesters: 0

Security: 0

Stars: 6

Watchers: 5

Forks: 7

Type:cakephp-plugin

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

This package is auto-updated.

Last update: 2024-10-26 06:04:37 UTC


README

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)

Install

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

Usage

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

use Cake\ORM\Table;

class CooksTable extends Table
{
    public function initialize($config)
    {
        $this->setTable('sti_cooks');
        $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.
        $this->setEntityClass('App\Model\Entity\AssistantChef');
    }
}

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
$cooks->newChef([...]);

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

// or, using the child table
$chefs->newEntity([...]);

Note

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

http://github.com/usemuffin/sti/issues

License

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