Comments (11)
The second parameter is the model instance, so you can handle this on your own.
from tenanti.
I figured another workaround.
To change between different tenants databases and master DB (but in same connection : host, user, password), I can use the follow snippet
\DB::disconnect(config('database.default'));
config(['database.connections.mysql.database'=>$database_name]);
Thank for great package.
from tenanti.
Thank @crynobone for your quick response.
Sorry for not clearly describe my situation. I'm using the multi database mode. The tenant table is the company that my clients (user table) belong to. Each user belongTo only one company.
As far as I know, I intend to handle the database connection via middleware, such as:
public function handle($request, Closure $next)
{
$user = Auth::user();
Tenanti::driver('company')->asDefaultConnection($user->company,'tenants_{id}'));
...
I can't see your mentioned model instance in that implementation.
from tenanti.
Dig into the code, bindWithKey
already handle everything
from tenanti.
https://github.com/orchestral/tenanti/blob/master/src/Migrator/Operation.php#L199
from tenanti.
tenanti/src/Migrator/Operation.php
Line 195 in 2cac500
When I try to implement my own string, such as tenants_<some-uuid>
, the 195th line captured it and transform to something like tenants_<some-uuid>_{id}
. (shared config is set to false for multi dabase mode).
from tenanti.
So I supposed the database name can have the format like : prefix_<uu-id>_{id}
in the mean time, right ?
from tenanti.
I feel lost here, so you telling me that the database connection name need to support other than model key? the same database connection name that Laravel provide by default as sqlite
, mysql
?
https://github.com/orchestral/tenanti/blob/master/README.md#database-connection-resolver you can already customize the database name here. Connection name should be irrelevant.
from tenanti.
Sorry,
Here is my wanting setup:
I have a master database, using 'mysql' connection setting.
In master database I implement Companies and Users tables, in which companies is tenants reference table.
So for each company, I'd like to have a tenants_{uuid} database, instead of tenants_{id}, because when I migrate:refresh the master database and re seed it, the old databases could be lost. Maybe this thing can not be done now. So I decide to go back to your initial solution, which is tenants_{id} format for auto generation database name. (Following your proposal code at https://github.com/crynobone/todoist/blob/multi-database/app/Observers/User.php)
- All tenants database shared the same config from dabase.connection.tenants.*
from tenanti.
I don't see any reason why you can't do anything such as https://github.com/crynobone/todoist/blob/multi-database/app/Observers/User.php#L43-L61
/**
* Create database for entity.
*
* @param \Illuminate\Database\Eloquent\Model $entity
*
* @return mixed
*/
protected function createTenantDatabase(Model $entity)
{
$connection = $entity->getConnection();
$driver = $connection->getDriverName();
switch ($driver) {
case 'mysql':
$query = "CREATE DATABASE `todoist_{$entity->uuid}`";
break;
case 'pgsql':
$query = "CREATE DATABASE todoist_{$entity->uuid}";
break;
default:
throw new InvalidArgumentException("Database Driver [{$driver}] not supported");
}
return $connection->unprepared($query);
}
And resolve the Database connection based on https://github.com/crynobone/todoist/blob/multi-database/app/Providers/EventServiceProvider.php#L37-L41
Tenanti::connection('tenants', function (User $entity, array $config) {
$config['database'] = "todoist_{$entity->uuid}";
return $config;
});
from tenanti.
Thank you @crynobone ,
I'm using your 3.4.x@dev version.
I did put the connection logic to AppServiceProvider as mentioned in the guide, and the log do triggered. But only when I manage to intercept a customized middleware as mentioned in https://github.com/orchestral/tenanti#setting-default-database-connection, the connection then goes well.
Below is my implementation:
//for AppServiceProvider.php
$CI=$this;
Tenanti::connection('tenants', function (Model $entity, array $config) use ($CI) {
$id=($entity instanceof User)?$entity->company->id:(($entity instanceof Company)?$entity->id:1);
$config['database'] = 'tenants_'.$id;
Log::info($entity);
return $config;
});
//for TenantResolveMiddleware.php
//Company $entity;
Tenanti::driver('company')->asDefaultConnection($entity,'tenants_{id}'));
I figured that only the asDefaultConnection method can set tenants' models to automatically retrieve from tenants' tables. And the resolver Tenanti::connection only store the database configuration. If the resolver from AppServiceProvider is missing, the app will return the error that database {...} is not configured. So is this the right way? It is supposed that Tenanti::connection automatically set the database connection to the tenant config, isn't it?
from tenanti.
Related Issues (20)
- Noob question about getting data HOT 2
- How to queue a specify tenant HOT 4
- Update to laravel 5.5 HOT 2
- Using seeders in Single Database HOT 2
- specific tenant for some models
- Too Many Connections HOT 16
- Multiple Database - Set connection using ID instead of model HOT 2
- Migrating for one specific tenant HOT 2
- Get main databse HOT 1
- Run laravel dusk on one of tenants HOT 1
- When can I setup this package ? HOT 2
- [3.7] Display messages during running migrator
- Release support for Laravel 6 HOT 1
- [5.x] Update Migrator to use Laravel default Migrator::usingConnection() method.
- Setup github workflow
- [Job] How to use with the running job?? HOT 2
- config file was not loaded HOT 1
- Issue with laravel 6.x/7.x/8 Tenanti Version 5.0&6 HOT 10
- [Discussion] Creating a tenancy based on table not on domain.
- Incompatible with Laravel 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tenanti.