From 2556ab8d7b874eeb8c662d092bc91d1b52535cc0 Mon Sep 17 00:00:00 2001 From: Vincent Prat Date: Thu, 16 Dec 2021 12:01:07 +0100 Subject: [PATCH 1/3] Allow table inspection in several database connections at once fixes #1141 Currently the package cannot handle schema discovery in more than a single database at once. In multi-tenant applications it is very frequent that there are at least two databases active at some point in time: landlord (data common to all tenants) and tenant (specific to one tenant). When the configuration variable `db_mapping` is not specified, it defaults to null which then makes the code strictly behave as before. Hence this introduces no breaking change. --- src/Console/ModelsCommand.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index c3c08c445..2aaf0cd12 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -467,8 +467,9 @@ public function getPropertiesFromTable($model) } $databasePlatform->registerDoctrineTypeMapping($yourTypeName, $doctrineTypeName); } - - $database = null; + + $connectionName = $model->getConnection()->getName(); + $database = config("ide-helper.db_mapping.$connectionName"); if (strpos($table, '.')) { [$database, $table] = explode('.', $table); } From 1e735824b420b02580ce1dce0a4de8ba4f18fe3a Mon Sep 17 00:00:00 2001 From: Vincent Prat Date: Thu, 16 Dec 2021 12:07:35 +0100 Subject: [PATCH 2/3] Add optional configuration for multi-database inspection --- config/ide-helper.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/config/ide-helper.php b/config/ide-helper.php index 51fcdf24a..605bfdda8 100644 --- a/config/ide-helper.php +++ b/config/ide-helper.php @@ -316,4 +316,26 @@ // 'ide-helper:models --nowrite', ], + /* + |-------------------------------------------------------------------------- + | Allow discovery in multiple active databases + |-------------------------------------------------------------------------- + | + | When running IDE helper in a multi-tenant context, you would need to + | indicate the database to match the different connections used by your + | models. + | + | This is required if your application has models dispatched both in a common + | database (called landlord or system depending on the libraries) and in a + | tenant specific database (one which will contain data which can vary between + | tenants) + | + | In other cases (single database or no common database), you can leave + | this commented. + */ +// 'db_mapping'=> [ +// // laravel connection name => real database name +// 'landlord' => 'homestead', +// 'tenant' => 'test_tenant', +// ], ]; From 56d89ac1e4c26188ca2894b9a9bbc3b8c15cd0f2 Mon Sep 17 00:00:00 2001 From: Vincent Prat Date: Thu, 16 Dec 2021 14:42:23 +0100 Subject: [PATCH 3/3] Remove usage of global config helper --- src/Console/ModelsCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 2aaf0cd12..a723326a4 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -469,7 +469,7 @@ public function getPropertiesFromTable($model) } $connectionName = $model->getConnection()->getName(); - $database = config("ide-helper.db_mapping.$connectionName"); + $database = $this->laravel['config']->get("ide-helper.db_mapping.$connectionName"); if (strpos($table, '.')) { [$database, $table] = explode('.', $table); }