Git Product home page Git Product logo

cakephp-table-inheritance's Introduction

CakePHP Table Inheritance plugin

Latest Stable Version Total Downloads Software License Build Status codecov

This plugin implements Single Table Inheritance (and hopefully will implement Class Table Inheritance in the future) patterns for CakePHP ORM.

Installation

CakePHP 4.x

Using composer:

composer require robotusers/cakephp-table-inheritance

For CakePHP 3.x use version 0.4 of the plugin

composer require robotusers/cakephp-table-inheritance:^0.4

StiBehavior

For now only STI is supported. Just add a behavior to your tables:

//in ClientsTable:
public function initialize(array $config)
{
    $this->addBehavior('Robotusers/TableInheritance.Sti', [
        'table' => 'users',
        'discriminator' => 'client'
    ]);
}

//alternative config in AdministratorsTable:
public function initialize(array $config)
{
    $this->table('users');
    $this->addBehavior('Robotusers/TableInheritance.Sti');
    $this->setDiscriminator('admin');
}

Now both the ClientsTable and AdministratorsTable will share users db table. A table has to have a discriminator field which will be used to determine which model's record is stored in a row.

Multiple discriminators

You can also configure a list of allowed discriminators. It's useful for example when working with the files. For example:

//in ImagesTable:
public function initialize(array $config)
{
    $this->addBehavior('Robotusers/TableInheritance.Sti', [
        'table' => 'files',
        'discriminatorField' => 'mime',
        'acceptedDiscriminators' => [
            'image/jpeg',
            'image/gif',
            'image/png',
            'image/tiff'
        ]
    ]);
}

//or using wildcards:

public function initialize(array $config)
{
    $this->addBehavior('Robotusers/TableInheritance.Sti', [
        'table' => 'files',
        'discriminatorField' => 'mime',
        'acceptedDiscriminators' => [
            'image/*'
        ]
    ]);
}

An ImagesTable will share files db table and match only specified mime types.

You can also add accepted discriminators on runtime:

$table->addAcceptedDiscriminator('image/bmp');

Configuration

StiBehavior supports following options:

  • discriminatorField - db table field used to discriminate models, 'discriminator' by default
  • discriminator - default discriminator value, $table->alias() by default
  • table - db table to share, use this option or $table->table() method.
  • checkRules - true by default. Allows to enable/disable build-in rule check for a discriminator value.
  • acceptedDiscriminators - a list of accepted discriminators.

StiParentBehavior

This plugin also allows to configure parent Table in order to create and hydrate entities based on child tables.

//in UsersTable:
public function initialize(array $config)
{
    $this->addBehavior('Robotusers/TableInheritance.StiParent', [
        'tableMap' => [
            'Administrators' => [
                'admin',
                'administrator'
            ],
            'Clients' => 'client'
        ]
    ]);
}

tableMap option accepts an array mapping table registry aliases to discriminator field values.

You can also map discriminator values to specified table objects using discriminatorMap option:

//in UsersTable:
public function initialize(array $config)
{
    $this->addBehavior('Robotusers/TableInheritance.StiParent', [
        'discriminatorMap' => [
            'admin' => $this->tableLocator()->get('Administrators'),
            'client' => $this->tableLocator()->get('Clients')
        ]
    ]);
}

This behavior also provides newStiEntity() method which will proxy newEntity() to one of the configured tables based on a discriminator value.

$data = [
    'name' => 'super-admin',
    'discriminator' => 'admin'
];

$admin = $this->Users->newStiEntity($data); //will call AdministratorsTable::newEntity() and return an Administrator entity instance.

Afterwards you can get a STI table using stiTable() method and handle entity using its source Table object.

$table = $this->Users->stiTable($admin); 
$table->save($admin); //it will save an entity using AdministratorsTable

You can also directly detect STI table from data array:

$data = [
    'name' => 'super-admin',
    'discriminator' => 'admin'
];

$table = $this->Users->stiTable($data);
$admin = $table->newEntity($data);
$table->save($admin);

cakephp-table-inheritance's People

Contributors

josephshanak avatar robertpustulka avatar themrwilliams avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

cakephp-table-inheritance's Issues

Query containing hasOne association pointing to a class having the StiParent behavior triggers error if no associated data is found

The StiParentBehavior, in its beforeFind() method, registers a formatter with formatResults(), in which it does its magic.

If I run a query on some other main table, having a hasOne association defined to the table with the StiParentBehavior, and I contain that association, then the following error occurs if one of the results does not have such an hasOne related record:
Argument 1 passed to Robotusers\TableInheritance\Model\Behavior\StiParentBehavior::Robotusers\TableInheritance\Model\Behavior\{closure}() must implement interface Cake\Datasource\EntityInterface, null given

The reason:

  • The default strategy of hasOne associations is join
  • Thus, the sql statement for the main query gets additional code added for a JOIN to the associated table, so their fields are directly loaded with the main query
  • In the Association class, the method _formatAssociationResults() is executed, amongst others, with the following code:
$property = $options['propertyPath'];
$propertyPath = explode('.', $property);
$query->formatResults(function ($results, $query) use ($formatters, $property, $propertyPath) {
    $extracted = [];
    foreach ($results as $result) {
        foreach ($propertyPath as $propertyPathItem) {
            if (!isset($result[$propertyPathItem])) {
                $result = null;
                break;
            }
            $result = $result[$propertyPathItem];
        }
        $extracted[] = $result;
    }
    $extracted = new Collection($extracted);
    foreach ($formatters as $callable) {
        $extracted = new ResultSetDecorator($callable($extracted, $query));
    }
    ...
  • If there is no hasOne related record, then $result[$propertyPathItem] is null. This leads to $result being set to null, and that being added to $extracted. For a hasOne association, as it can be max. one result, $extractedwill then be:
[
    0 => null,
]
  • This is then sent to each formatter. One of which being the formatResults() defined in the beforeFind() of StiParentBehavior.
  • It does a $results->map(function (EntityInterface $row) {...}). $resultsonly has one element, and that has the value null, so consequently the error occurs, because the function within $results->map(...) expects the parameter $row to implement the `EntityInterface´.

If I change the strategy of the hasOne assocation to select. Then this works. If the associated data is retrieved with a separate query, then _formatAssociationResults() is not executed, and the error is not triggered.

I am wondering, is that a conceptual issue, an issue on the way CakePHP 4 does with hasOne associated data if it is JOINed to the main query? Or would it be feasible to amend the StiParentBehavior? I am not sure if that could cause any other problems somewhere else, or whether it was done for a particular reason to force $row to implement the EntityInterface and not be null.

CakePHP 4 Support

I am currently using this plugin in a CakePHP3 application that is preparing to upgrade to CakePHP 4, and I would like to know what your plans are for maintaining this plugin now that CakePHP 4 is out. Are there plans on updating this plugin to support CakePHP 4?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.