# Multi tenant - Shared Database, Different Schema

**URL:** https://discourse.cakephp.org/t/multi-tenant-shared-database-different-schema/1993
**Category:** Need Help
**Created:** [February 5, 2017, 11:20pm UTC](https://discourse.cakephp.org/t/multi-tenant-shared-database-different-schema/1993 "2017-02-05T23:20:46Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![elatos](https://avatars.discourse-cdn.com/v4/letter/e/b487fb/32.png) [@elatos](https://discourse.cakephp.org/u/elatos)
#### Post date: [February 5, 2017, 11:20pm UTC](https://discourse.cakephp.org/t/multi-tenant-shared-database-different-schema/1993/1 "2017-02-05T23:20:46Z")

</div>

I am developing an application that uses a central user table  
for user authentication. After connection i would like each user to have a different schema.  
(multi tenant strategy - shared database, different schema)

This lead us to have one model to handle multiple tables with same structure but different name.  
i.e BookmarksTable model for ‘Bookmarks\_tenantA’ and ‘Bookmarks\_tenantB’ tables

I am looking for a way to pass the tenant\_id into the initialize model function.  
In that way i could initialize - handle each time different table.

i have found the following links

> [@Implementing SaaS (Multi Tenant) Model](http://discourse.cakephp.org/t/implementing-saas-multi-tenant-model/332):
>
> Our CakePHP 3 app (a POS System) is going to use subdomain approach where clients will share a common code base but run on individual databases. 2 things from the config (app.php) file will be unique to each instance : database details and security salt. These details will be stored in a table of main site database (wordpress) and I want to load them based on the subdomain opened. ([abc.maindomain.com](http://abc.maindomain.com) or [xyz.maindomain.com](http://xyz.maindomain.com)) I am not sure where should I implement this stuff in the code. bootstra…

> <https://stackoverflow.com/questions/5851635/how-to-send-data-form-controller-to-other-model>

> <https://stackoverflow.com/questions/4681247/in-cakephp-can-we-dynamically-change-the-table-linked-to-a-particular-model>

But i could n’t extract any useful information.

---

<div class="post-metadata">

### Author: ![paulop](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/paulop/32/642_2.png) [@paulop](https://discourse.cakephp.org/u/paulop)
#### Post date: [May 23, 2017, 3:21pm UTC](https://discourse.cakephp.org/t/multi-tenant-shared-database-different-schema/1993/2 "2017-05-23T15:21:49Z")

</div>

Hello, I’m using a structure just like yours.

Inside the Controller in the beforeFilter Event I put the schema.table name like that.

```
CadastrosController extends AppController
{
    public function beforeFilter(Event $event) 
    {
        $session = $this->request->session();
        $empresa = $session->read('Empresa');
        if (isset($empresa->schemaname)){
            $this->Cadastros->table($empresa->schemaname.'.cadastros');
        }
....

```

My difficulty is when I create a new registry and it is necessary to create a new schema.

What is your vision when we have many registrations and many schemas … will it become a problem in the future?  
Anyone else have any ideas about this?

This [post](http://discourse.cakephp.org/t/implementing-saas-multi-tenant-model/332) is interesting, but with multiple databases mysql.
