Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/UseMuffin/Sti

Single Table Inheritance for CakePHP ORM.
https://github.com/UseMuffin/Sti

Last synced: about 1 month ago
JSON representation

Single Table Inheritance for CakePHP ORM.

Awesome Lists containing this project

README

        

# Sti

[![Build Status](https://img.shields.io/travis/UseMuffin/Sti/master.svg?style=flat-square)](https://travis-ci.org/UseMuffin/Sti)
[![Coverage](https://img.shields.io/coveralls/github/UseMuffin/Sti?style=flat-square)](https://codecov.io/gh/UseMuffin/Sti)
[![Total Downloads](https://img.shields.io/packagist/dt/muffin/sti.svg?style=flat-square)](https://packagist.org/packages/muffin/sti)
[![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](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][1])

## Install

Using [Composer][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
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
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`:

```php
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][standards].

## Bugs & Feedback

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

## License

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

[cakephp]:http://cakephp.org
[composer]:http://getcomposer.org
[mit]:http://www.opensource.org/licenses/mit-license.php
[muffin]:http://usemuffin.com
[standards]:http://book.cakephp.org/3.0/en/contributing/cakephp-coding-conventions.html
[1]:https://en.wikipedia.org/wiki/Single_Table_Inheritance