{"id":14966681,"url":"https://github.com/jlorente/yii2-activerecord-inheritance","last_synced_at":"2025-06-10T22:04:30.889Z","repository":{"id":57000176,"uuid":"31781465","full_name":"jlorente/yii2-activerecord-inheritance","owner":"jlorente","description":"ActiveRecord Inheritance is an util to provide the Class Table Inheritance Pattern the to the Yii2 framework","archived":false,"fork":false,"pushed_at":"2023-11-15T19:05:14.000Z","size":28,"stargazers_count":18,"open_issues_count":7,"forks_count":7,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-05-23T00:37:01.832Z","etag":null,"topics":["activerecord","activerecord-inheritance","inheritance","php","yii2-framework"],"latest_commit_sha":null,"homepage":null,"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/jlorente.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","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,"publiccode":null,"codemeta":null}},"created_at":"2015-03-06T17:51:09.000Z","updated_at":"2023-11-09T10:06:47.000Z","dependencies_parsed_at":"2024-09-14T02:54:45.894Z","dependency_job_id":null,"html_url":"https://github.com/jlorente/yii2-activerecord-inheritance","commit_stats":{"total_commits":16,"total_committers":2,"mean_commits":8.0,"dds":0.0625,"last_synced_commit":"7f6f28609fbbb8f4e4d604047977af329ea6bd1c"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jlorente%2Fyii2-activerecord-inheritance","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jlorente%2Fyii2-activerecord-inheritance/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jlorente%2Fyii2-activerecord-inheritance/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jlorente%2Fyii2-activerecord-inheritance/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jlorente","download_url":"https://codeload.github.com/jlorente/yii2-activerecord-inheritance/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jlorente%2Fyii2-activerecord-inheritance/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259159655,"owners_count":22814492,"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":["activerecord","activerecord-inheritance","inheritance","php","yii2-framework"],"created_at":"2024-09-24T13:36:47.735Z","updated_at":"2025-06-10T22:04:30.863Z","avatar_url":"https://github.com/jlorente.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"ActiveRecord Inheritance\n========================\n\nActiveRecord Inheritance is a util to provide the [Class Table Inheritance Pattern](http://martinfowler.com/eaaCatalog/classTableInheritance.html) to the Yii2 framework. Its motivation is to fake inheritance between two ActiveRecord classes.\n\n## Installation\n\nInclude the package as dependency under the bower.json file.\n\nTo install, either run\n\n```bash\n$ php composer.phar require jlorente/yii2-activerecord-inheritance \"*\"\n```\n\nor add\n\n```json\n...\n    \"require\": {\n        ...\n        \"jlorente/yii2-activerecord-inheritance\": \"*\"\n    }\n```\n\nto the ```require``` section of your `composer.json` file.\n\n## Usage\n\nAn example of usage could be:\n\nSuppose you have the following schema.\n```sql\nCREATE TABLE `user` (\n  `id` int(11) NOT NULL AUTO_INCREMENT,\n  `username` varchar(255) COLLATE utf8_unicode_ci NOT NULL,\n  `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,\n  `name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,\n  `last_name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,\n  `created_at` int(11) NOT NULL,\n  `updated_at` int(11) NOT NULL,\n  PRIMARY KEY (`id`)\n);\n\nCREATE TABLE `admin` (\n  `id` int(11) NOT NULL AUTO_INCREMENT,\n  `level` INT NOT NULL,\n  `banned_users` INT NOT NULL DEFAULT 0,\n  PRIMARY KEY (`id`),\n  CONSTRAINT `FK_Admin_Id` FOREIGN KEY (`id`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,\n);\n```\n\nTo fake the inheritance between the two tables your code must look like this.\n\n```php\nuse jlorente\\db\\ActiveRecordInheritanceTrait,\n    jlorente\\db\\ActiveRecordInheritanceInterface;\nuse yii\\db\\ActiveRecord;\n\nclass User extends ActiveRecord {\n\n    public function tableName() {\n        return 'user';\n    }\n\n    public function doSomething() {\n        echo 'User does something';\n    }\n}\n\nclass Admin extends ActiveRecord implements ActiveRecordInheritanceInterface {\n    use ActiveRecordInheritanceTrait;\n\n    public function tableName() {\n        return 'admin';\n    }\n\n    public static function extendsFrom() {\n        return User::className();\n    }\n\n    public function doSomething() {\n        echo 'Admin does something';\n    }\n}\n```\n\nAnd you will be able to use Admin objects as if they were inheriting from User objects.\n\n```php\n$admin = new Admin();\n$admin-\u003eusername = 'al-acran';\n$admin-\u003eemail = 'al_acran@gmail.com';\n$admin-\u003ename = 'Al';\n$admin-\u003elast_name = 'Acran';\n$admin-\u003elevel = 1;\n$admin-\u003esave();\n```\n\nYou can call parent methods and properties by using the parent relation property.\n```php\n$admin-\u003edoSomething()           //Admin does something\n$admin-\u003eparent-\u003edoSomething()   //User does something\n```\n\nThis trait is very useful for faking inheritance, however query filters should be applied on the parent relation.\n```php\n$admin = Admin::find()\n    -\u003ejoinWith('parent', function($query) {\n        $query-\u003eandWhere(['username' =\u003e 'al-acran']);\n        $query-\u003eandWhere(['name' =\u003e 'Al']);\n    })\n    -\u003eandWhere(['level' =\u003e 1])\n    -\u003eone();\n```\n\n## Considerations\nIn order to use the trait properly, you must consider the following points\n\n### General\n* By default, the primary key of the supposed child class is used as foreign key of the parent model, if you want to use another attribute as foreign key, yoy should overwrite the parentAttribute() method.\n* The trait won't work with multiple primary and foreign keys.\n\n### ActiveRecord methods\n* Methods overwriten from ActiveRecord in this trait like save, validate, etc... should not be overwriten on the class that uses the trait or functionality will be lost. \n* To overwrite this methods properly, you could extend the class that uses the trait and overwrite there the methods, making sure that all methods call its parent implementation.\n\n### Inheritance call hierarchy\n* The natural call hierarchy is preserved, so if a method or property exists on the called class or one of its ancestors, the method or property is called.\n* If a method or property doesn't exist in the natural call hierarchy. The properly magic method is called (__get, __set, __isset, __unset, __call) and the yii2 call hierarchy is used. If the method or property isn't found in this call hierarchy the next step is executed.\n* The previous process is repeteated for the faked parent object.\n* The call hierarchy will stop when a method or property is found or when there are no more parents. In this case, an UnknownPropertyException or an UnknownMethodException will be raised.\n* You can concatenate faked parent classes with the only limit of the php call stack.\n\n## License \nCopyright \u0026copy; 2015 José Lorente Martín \u003cjose.lorente.martin@gmail.com\u003e.\nLicensed under the MIT license. See LICENSE.txt for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjlorente%2Fyii2-activerecord-inheritance","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjlorente%2Fyii2-activerecord-inheritance","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjlorente%2Fyii2-activerecord-inheritance/lists"}