https://github.com/solutosoft/yii-multitenant
Active Record MultiTenant Extension
https://github.com/solutosoft/yii-multitenant
activerecord multitenancy multitenant yii2 yii2-extension
Last synced: 8 months ago
JSON representation
Active Record MultiTenant Extension
- Host: GitHub
- URL: https://github.com/solutosoft/yii-multitenant
- Owner: solutosoft
- Created: 2019-04-28T10:48:53.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-02-26T14:13:18.000Z (over 5 years ago)
- Last Synced: 2025-01-31T20:55:49.094Z (8 months ago)
- Topics: activerecord, multitenancy, multitenant, yii2, yii2-extension
- Language: PHP
- Homepage:
- Size: 37.1 KB
- Stars: 8
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
Active Record MultiTenant Extension
=====================================This extension provides support for ActiveRecord MultiTenant.
[](https://travis-ci.org/solutosoft/yii-multitenant)
[](https://scrutinizer-ci.com/g/solutosoft/yii-multitenant/?branch=master)
[](https://scrutinizer-ci.com/g/solutosoft/yii-multitenant/?branch=master)
[](https://packagist.org/packages/solutosoft/yii-multitenant)
[](https://packagist.org/packages/solutosoft/yii-multitenant)Installation
------------The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
Either run
```
php composer.phar require --prefer-dist solutosoft/yii-multitenant
```or add
```json
"solutosoft/yii-multitenant": "*"
```Usage
-----1. Creates table with `tenant_id` column:
```php
class m191023_101232_create_post extends Migration
{
/**
* {@inheritdoc}
*/
public function up()
{
$this->createTable('post', [
'id' => $this->primaryKey(),
'title' => $this->string()->notNull(),
'category_id' => $this->integer(),
'content' => $this->string()
'tenant_id' => $this->integer(),
]);
$this->createTable('category', [
'id' => $this->primaryKey(),
'name' => $this->string()->notNull(),
'tenant_id' => $this->integer(),
]);
}
}```
2. Adds `TenantInterface` to user model:```php
use solutosoft\multitenant\MultiTenantRecord;class User extends MultiTenantRecord implements IdentityInterface, TenantInterface
{
/**
* {@inheritdoc}
*/
public function getTenantId()
{
return // logic to determine tenant from current user
}
/**
* Finds user by username attribute
* This is an example where tenant filter is disabled
*/
public static function findByUsername($username)
{
return static::find()->withoutTenant()->where(['username' => $username]);
}
...
}
```3. Extends models with `tenant_id` attribute from `MultiTenantRecord` intead of `ActiveRecord`:
```php
use solutosoft\multitenant\MultiTenantRecord;class Post extends MultiTenantRecord
{
...
}class Category extends MultiTenantRecord
{
...
}
```Now when you save or execute some query the `tenant_id` column will be used. Example:
```php
// It's necessary the user will be logged in$posts = \app\models\Post::find()->where(['category_id' => 1])->all();
// SELECT * FROM `post` WHERE `category_id` = 1 and `tenant_id` = 1;$category = \app\models\Category([
'name' => 'framework'
]);
$category->save();
// INSERT INTO `category` (`name`, `tenant_id`) values ('framework', 1);