1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 16:14:03 +00:00

use Laravel's connection getter and setter

This commit is contained in:
Abrar Ahmad 2022-08-31 11:16:10 +05:00
parent 0e72c71286
commit cc2df5c0b6

View file

@ -23,33 +23,18 @@ class BatchTenancyBootstrapper implements TenancyBootstrapper
$batchRepository = app(BatchRepository::class);
if ($batchRepository instanceof DatabaseBatchRepository) {
/**
* Access the resolved batch repository instance and update its connection to use the tenant connection
*/
$batchRepositoryReflection = new ReflectionClass($batchRepository);
$connectionProperty = $batchRepositoryReflection->getProperty('connection');
$connectionProperty->setAccessible(true);
$connection = $connectionProperty->getValue($batchRepository);
$this->previousConnection = $connection;
$connectionProperty->setValue($batchRepository, DB::connection('tenant'));
// Access the resolved batch repository instance and update its connection to use the tenant connection
$this->previousConnection = $batchRepository->getConnection();
$batchRepository->setConnection(DB::connection('tenant'));
}
}
public function revert()
{
if ($this->previousConnection) {
/**
* Access the resolved batch repository instance and replace its connection with the previously replaced one
*/
// Access the resolved batch repository instance and replace its connection with the previously replaced one
$batchRepository = app(BatchRepository::class);
$batchRepositoryReflection = new ReflectionClass($batchRepository);
$connectionProperty = $batchRepositoryReflection->getProperty('connection');
$connectionProperty->setAccessible(true);
$connectionProperty->setValue($batchRepository, $this->previousConnection);
$batchRepository->setConnection($this->previousConnection);
$this->previousConnection = null;
}
}