Let’s say my application has many databases.
One shared database with 2 tables:
- Users: id, email, password and company_id
- Company: id, name and database
Each company has its own database with a table called Records, all of them with the exact same structure.
- Records: id, date, score
This is my working code
AppController.php:
$identity = $this->Authentication->getIdentity();
if ($identity) {
Configure::write("database", $identity->get('company')->get('database'));
}
RecordsTable.php
public static function defaultConnectionName(): string
{
return Configure::read("database");
}
When a user logs in, they can see the records of their own company.
The link example.com/records might connect to a different database and thereby show different results according to each user.
My problem is that some users want to access data from another companies, so I’m trying to use the prefix to define which database should I use. For example:
- example.com/1/records will access to all the records of the company with id 1
- example.com/2/records will access to all the records of the company with id 2
Has anyone solved a similar problem? I would like to know how to approach since I cant find the right tools on routes.php.
Thanks in advance!