{"id":14971300,"url":"https://github.com/solutosoft/yii-multitenant","last_synced_at":"2025-10-26T14:31:45.200Z","repository":{"id":57055410,"uuid":"183895457","full_name":"solutosoft/yii-multitenant","owner":"solutosoft","description":"Active Record MultiTenant Extension","archived":false,"fork":false,"pushed_at":"2020-02-26T14:13:18.000Z","size":38,"stargazers_count":8,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-31T20:55:49.094Z","etag":null,"topics":["activerecord","multitenancy","multitenant","yii2","yii2-extension"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/solutosoft.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-04-28T10:48:53.000Z","updated_at":"2024-06-12T13:05:28.000Z","dependencies_parsed_at":"2022-08-24T14:00:17.191Z","dependency_job_id":null,"html_url":"https://github.com/solutosoft/yii-multitenant","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/solutosoft%2Fyii-multitenant","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/solutosoft%2Fyii-multitenant/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/solutosoft%2Fyii-multitenant/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/solutosoft%2Fyii-multitenant/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/solutosoft","download_url":"https://codeload.github.com/solutosoft/yii-multitenant/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238347892,"owners_count":19457012,"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","multitenancy","multitenant","yii2","yii2-extension"],"created_at":"2024-09-24T13:44:59.524Z","updated_at":"2025-10-26T14:31:39.903Z","avatar_url":"https://github.com/solutosoft.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Active Record MultiTenant Extension\n=====================================\n\nThis extension provides support for ActiveRecord MultiTenant.\n\n[![Build Status](https://travis-ci.org/solutosoft/yii-multitenant.svg?branch=master)](https://travis-ci.org/solutosoft/yii-multitenant)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/solutosoft/yii-multitenant/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/solutosoft/yii-multitenant/?branch=master)\n[![Code Coverage](https://scrutinizer-ci.com/g/solutosoft/yii-multitenant/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/solutosoft/yii-multitenant/?branch=master)\n[![Total Downloads](https://poser.pugx.org/solutosoft/yii-multitenant/downloads.png)](https://packagist.org/packages/solutosoft/yii-multitenant)\n[![Latest Stable Version](https://poser.pugx.org/solutosoft/yii-multitenant/v/stable.png)](https://packagist.org/packages/solutosoft/yii-multitenant)\n\nInstallation\n------------\n\nThe preferred way to install this extension is through [composer](http://getcomposer.org/download/).\n\nEither run\n\n```\nphp composer.phar require --prefer-dist solutosoft/yii-multitenant\n```\n\nor add\n\n```json\n\"solutosoft/yii-multitenant\": \"*\"\n```\n\nUsage\n-----\n\n1. Creates table with `tenant_id` column:\n\n```php\nclass m191023_101232_create_post extends Migration\n{\n    /**\n     * {@inheritdoc}\n     */\n    public function up()\n    {\n        $this-\u003ecreateTable('post', [\n            'id' =\u003e  $this-\u003eprimaryKey(),\n            'title' =\u003e $this-\u003estring()-\u003enotNull(),\n            'category_id' =\u003e $this-\u003einteger(),\n            'content' =\u003e $this-\u003estring()            \n            'tenant_id' =\u003e $this-\u003einteger(),\n        ]);\n        \n        $this-\u003ecreateTable('category', [\n            'id' =\u003e  $this-\u003eprimaryKey(),\n            'name' =\u003e $this-\u003estring()-\u003enotNull(),            \n            'tenant_id' =\u003e $this-\u003einteger(),\n        ]);\n    }\n}\n\n```\n2. Adds `TenantInterface` to user model:\n\n```php\nuse solutosoft\\multitenant\\MultiTenantRecord;\n\nclass User extends MultiTenantRecord implements IdentityInterface, TenantInterface    \n{\n    /**\n     * {@inheritdoc}\n     */\n    public function getTenantId()\n    {\n        return // logic to determine tenant from current user\n    }\n    \n    /**\n     * Finds user by username attribute\n     * This is an example where tenant filter is disabled\n     */\n    public static function findByUsername($username)\n    {\n        return static::find()-\u003ewithoutTenant()-\u003ewhere(['username' =\u003e $username]);\n    }\n    \n    ...\n    \n}\n```\n\n3. Extends models with `tenant_id` attribute from `MultiTenantRecord` intead of `ActiveRecord`: \n\n```php\nuse solutosoft\\multitenant\\MultiTenantRecord;\n\nclass Post extends MultiTenantRecord\n{    \n    ...   \n}\n\nclass Category extends MultiTenantRecord\n{    \n    ...   \n}\n```\n\nNow when you save or execute some query the `tenant_id` column will be used. Example:\n\n```php\n// It's necessary the user will be logged in\n\n$posts = \\app\\models\\Post::find()-\u003ewhere(['category_id' =\u003e 1])-\u003eall();\n// SELECT * FROM `post` WHERE `category_id` = 1 and `tenant_id` = 1;\n\n$category = \\app\\models\\Category([\n  'name' =\u003e 'framework'\n]);\n$category-\u003esave();\n// INSERT INTO `category` (`name`, `tenant_id`) values ('framework', 1);\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsolutosoft%2Fyii-multitenant","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsolutosoft%2Fyii-multitenant","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsolutosoft%2Fyii-multitenant/lists"}