https://github.com/originphp/multi_tenant
https://github.com/originphp/multi_tenant
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/originphp/multi_tenant
- Owner: originphp
- License: mit
- Created: 2019-09-16T07:31:40.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2021-01-05T12:27:15.000Z (about 5 years ago)
- Last Synced: 2025-03-02T09:45:16.993Z (11 months ago)
- Language: PHP
- Size: 22.5 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
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.