Now its working but when I visit records/index I’m getting:
“SQLSTATE[42S02]: Base table or view not found: 1146 Table ‘particular_database.groups’ doesn’t exist”
So I also added the next code but it’s still not working,
class RecordsTable extends Table
{
public function initialize(array $config): void
{
$this->belongsTo(‘Groups’, [
‘foreignKey’ => ‘group_id’,
//‘bindingKey’ => ???
‘strategy’ => ‘select’,
]);
}
}
class GroupsTable extends Table
{
public function initialize(array $config): void
{
parent::initialize($config);
$this->setTable(‘shared_database.groups’);
}
}
Thank you so much, that was what I was looking for. For anyone in the same situation, I changed the code from the first post to this so now I dont drop and replace the default connection anymore.
The config/app_local.php now has as ‘default’ the shared database and I create the connection to the particular databases dynamically.
public function beforeFilter(EventInterface $event)
{
if ($this->Auth->user()) {
Now the tables that belong to each particular database have one of the following code
public static function defaultConnectionName(): string {
return AppController::$particularDatabase;
}
and the tables from the shared database have:
public static function defaultConnectionName(): string {
return ‘default’;
}
pd: Im aware that saving the $particularDatabase as a static variable in AppController is not a good practice, but it will stay that way until I find another way to do it.