An open API service indexing awesome lists of open source software.

https://github.com/originphp/multi_tenant


https://github.com/originphp/multi_tenant

Last synced: 3 months ago
JSON representation

Awesome Lists containing this project

README

          

# Multi Tenant Plugin

The `MultiTenant` plugin enables you to make your web application multi-tenant using a single database.

## Installation

```linux
$ bin/console plugin:install originphp/multi_tenant
```

## Models

Add the `Tenantable` trait to any model and then add the column `tenant_id` to your database (and schema)

```php
use MultiTenant\Tenantable;

class Contact extends ApplicationModel
{
use Tenantable;
}
```

## Initializing MultiTenant

In your AppController initialize the Tenant after you have loaded the `AuthComponent`.

```php
use MultiTenant\Tenant;

class AppController extends Controller
{
protected function initialize() : void
{
$this->loadComponent('Auth');

if ($this->Auth->isLoggedIn()) {
$tenantId = $this->Auth->user('tenant_id');
Tenant::initialize($tenantId);
}
}
}
```

By default the Tenant class will use the `tenant_id` as the foreign key, if you want to use a different foreign key, then set this in the options when initializing the Tenant, here is an example:

```php
Tenant::initialize($userId, [
'foreignKey' => 'user_id'
]);
```

That is it, as long the `Tenant` instance has been initialized, any model that finds will add the `tenant id` to query, and when you create a record the tenant id will be added automatically.