{"id":13684431,"url":"https://github.com/robotusers/cakephp-table-inheritance","last_synced_at":"2026-01-07T16:54:30.240Z","repository":{"id":47729783,"uuid":"40288624","full_name":"robotusers/cakephp-table-inheritance","owner":"robotusers","description":"Single Table Inheritance implementation for CakePHP 3 ORM","archived":false,"fork":false,"pushed_at":"2024-03-22T09:05:29.000Z","size":54,"stargazers_count":5,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-09-16T19:10:05.782Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/robotusers.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2015-08-06T06:32:12.000Z","updated_at":"2022-06-01T01:25:04.000Z","dependencies_parsed_at":"2024-04-10T05:38:44.872Z","dependency_job_id":"9f445147-e4b0-432f-a720-844990b747b9","html_url":"https://github.com/robotusers/cakephp-table-inheritance","commit_stats":{"total_commits":74,"total_committers":6,"mean_commits":"12.333333333333334","dds":0.5540540540540541,"last_synced_commit":"30d7f65385d5fc5a007ca481d55cb90235120844"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robotusers%2Fcakephp-table-inheritance","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robotusers%2Fcakephp-table-inheritance/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robotusers%2Fcakephp-table-inheritance/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robotusers%2Fcakephp-table-inheritance/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/robotusers","download_url":"https://codeload.github.com/robotusers/cakephp-table-inheritance/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251785315,"owners_count":21643453,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T14:00:33.463Z","updated_at":"2026-01-07T16:54:30.184Z","avatar_url":"https://github.com/robotusers.png","language":"PHP","funding_links":[],"categories":["ORM / Database / Datamapping"],"sub_categories":[],"readme":"# CakePHP Table Inheritance plugin\n\n[![Latest Stable Version](https://poser.pugx.org/robotusers/cakephp-table-inheritance/v/stable)](https://packagist.org/packages/robotusers/cakephp-table-inheritance)\n[![Total Downloads](https://poser.pugx.org/robotusers/cakephp-table-inheritance/downloads)](https://packagist.org/packages/robotusers/cakephp-table-inheritance)\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](LICENSE)\n[![Build Status](https://travis-ci.org/robotusers/cakephp-table-inheritance.svg?branch=master)](https://travis-ci.org/robotusers/cakephp-table-inheritance)\n[![codecov](https://codecov.io/gh/robotusers/cakephp-table-inheritance/branch/master/graph/badge.svg)](https://codecov.io/gh/robotusers/cakephp-table-inheritance/branch/master)\n\n\nThis plugin implements [Single Table Inheritance](https://en.wikipedia.org/wiki/Single_Table_Inheritance) (and hopefully will implement Class Table Inheritance in the future) patterns for CakePHP ORM.\n\n## Installation\n\nCakePHP 4.x\n\nUsing composer:\n```\ncomposer require robotusers/cakephp-table-inheritance\n```\n\nFor CakePHP 3.x use version 0.4 of the plugin\n\n```\ncomposer require robotusers/cakephp-table-inheritance:^0.4\n```\n\n## StiBehavior\n\nFor now only STI is supported. Just add a behavior to your tables:\n```php\n//in ClientsTable:\npublic function initialize(array $config)\n{\n    $this-\u003eaddBehavior('Robotusers/TableInheritance.Sti', [\n        'table' =\u003e 'users',\n        'discriminator' =\u003e 'client'\n    ]);\n}\n\n//alternative config in AdministratorsTable:\npublic function initialize(array $config)\n{\n    $this-\u003etable('users');\n    $this-\u003eaddBehavior('Robotusers/TableInheritance.Sti');\n    $this-\u003esetDiscriminator('admin');\n}\n```\nNow 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.\n\n### Multiple discriminators ###\n\nYou can also configure a list of allowed discriminators. It's useful for example when working with the files.\nFor example:\n\n```php\n//in ImagesTable:\npublic function initialize(array $config)\n{\n    $this-\u003eaddBehavior('Robotusers/TableInheritance.Sti', [\n        'table' =\u003e 'files',\n        'discriminatorField' =\u003e 'mime',\n        'acceptedDiscriminators' =\u003e [\n            'image/jpeg',\n            'image/gif',\n            'image/png',\n            'image/tiff'\n        ]\n    ]);\n}\n\n//or using wildcards:\n\npublic function initialize(array $config)\n{\n    $this-\u003eaddBehavior('Robotusers/TableInheritance.Sti', [\n        'table' =\u003e 'files',\n        'discriminatorField' =\u003e 'mime',\n        'acceptedDiscriminators' =\u003e [\n            'image/*'\n        ]\n    ]);\n}\n```\n\nAn `ImagesTable` will share `files` db table and match only specified mime types.\n\nYou can also add accepted discriminators on runtime:\n\n```php\n$table-\u003eaddAcceptedDiscriminator('image/bmp');\n```\n\n### Configuration ###\n\n`StiBehavior` supports following options:\n\n* `discriminatorField` - db table field used to discriminate models, 'discriminator' by default\n* `discriminator` - default discriminator value, `$table-\u003ealias()` by default\n* `table` - db table to share, use this option or `$table-\u003etable()` method.\n* `checkRules` - `true` by default. Allows to enable/disable build-in rule check for a discriminator value.\n* `acceptedDiscriminators` - a list of accepted discriminators.\n\n## StiParentBehavior\n\nThis plugin also allows to configure parent Table in order to create and hydrate entities based on child tables.\n\n```php\n//in UsersTable:\npublic function initialize(array $config)\n{\n    $this-\u003eaddBehavior('Robotusers/TableInheritance.StiParent', [\n        'tableMap' =\u003e [\n            'Administrators' =\u003e [\n                'admin',\n                'administrator'\n            ],\n            'Clients' =\u003e 'client'\n        ]\n    ]);\n}\n```\n\n`tableMap` option accepts an array mapping table registry aliases to discriminator field values.\n\nYou can also map discriminator values to specified table objects using `discriminatorMap` option:\n\n```php\n//in UsersTable:\npublic function initialize(array $config)\n{\n    $this-\u003eaddBehavior('Robotusers/TableInheritance.StiParent', [\n        'discriminatorMap' =\u003e [\n            'admin' =\u003e $this-\u003etableLocator()-\u003eget('Administrators'),\n            'client' =\u003e $this-\u003etableLocator()-\u003eget('Clients')\n        ]\n    ]);\n}\n```\n\nThis behavior also provides `newStiEntity()` method which will proxy `newEntity()` to one of the configured tables based on a discriminator value.\n\n```php\n$data = [\n    'name' =\u003e 'super-admin',\n    'discriminator' =\u003e 'admin'\n];\n\n$admin = $this-\u003eUsers-\u003enewStiEntity($data); //will call AdministratorsTable::newEntity() and return an Administrator entity instance.\n```\n\nAfterwards you can get a STI table using `stiTable()` method and handle entity using its source `Table` object.\n\n```php\n$table = $this-\u003eUsers-\u003estiTable($admin); \n$table-\u003esave($admin); //it will save an entity using AdministratorsTable\n```\n\nYou can also directly detect STI table from data array:\n\n```php\n$data = [\n    'name' =\u003e 'super-admin',\n    'discriminator' =\u003e 'admin'\n];\n\n$table = $this-\u003eUsers-\u003estiTable($data);\n$admin = $table-\u003enewEntity($data);\n$table-\u003esave($admin);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobotusers%2Fcakephp-table-inheritance","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frobotusers%2Fcakephp-table-inheritance","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobotusers%2Fcakephp-table-inheritance/lists"}