dev-master
9999999-dev https://github.com/usemuffin/stiSingle Table Inheritance for CakePHP 3 ORM
MIT
The Development Requires
by ADmad
by Jad Bitar
by Others
cakephp muffin sti
Single Table Inheritance for CakePHP 3 ORM
Single Table Inheritance for CakePHP 3 ORM., (*2)
[...] 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., (*3)
(source: [Wikipedia][1]), (*4)
Using Composer:, (*5)
composer require muffin/sti:1.0.x-dev
You then need to load the plugin. You can use the shell command:, (*6)
bin/cake plugin load Muffin/Sti
or by manually adding statement shown below to bootstrap.php
:, (*7)
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->table('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->entityClass('App\Model\Entity\AssistantChef'); } }
Then create a class for every type of entity:, (*8)
The entity that was previously defined to be the 'default' one will need to use the StiAwareTrait
:, (*9)
<?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:, (*10)
<?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:, (*11)
// src/Model/Table/CooksTable.php public function validationChefs(Validator $validator) { // ... return $validator; }
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:, (*12)
```php // using the parent table $cooks->newChef([...]);, (*13)
// or, using the parent table again $cooks->newEntity(['type' => 'chef', ...]);, (*14)
// or, using the child table $chefs->newEntity([...]); ```, (*15)
For the above examples to work using (*chef), you need to add a custom rule to the Inflector
:, (*16)
Cake\Utility\Inflector::rules('plural', ['/chef$/i' => '\1Chefs']);
To ensure your PRs are considered for upstream, you MUST follow the CakePHP coding standards., (*17)
http://github.com/usemuffin/sti/issues, (*18)
Copyright (c) 2015, Use Muffin and licensed under The MIT License., (*19)
Single Table Inheritance for CakePHP 3 ORM
MIT
cakephp muffin sti