Coder Social home page Coder Social logo

Comments (11)

crynobone avatar crynobone commented on June 15, 2024 1

The second parameter is the model instance, so you can handle this on your own.

from tenanti.

nguyenhaiphan avatar nguyenhaiphan commented on June 15, 2024 1

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.

nguyenhaiphan avatar nguyenhaiphan commented on June 15, 2024

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.

crynobone avatar crynobone commented on June 15, 2024

Dig into the code, bindWithKey already handle everything

from tenanti.

crynobone avatar crynobone commented on June 15, 2024

https://github.com/orchestral/tenanti/blob/master/src/Migrator/Operation.php#L199

from tenanti.

nguyenhaiphan avatar nguyenhaiphan commented on June 15, 2024

if (substr($database, -5) !== '_{id}' && $this->getConfig('shared', true) === false) {

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.

nguyenhaiphan avatar nguyenhaiphan commented on June 15, 2024

So I supposed the database name can have the format like : prefix_<uu-id>_{id} in the mean time, right ?

from tenanti.

crynobone avatar crynobone commented on June 15, 2024

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.

nguyenhaiphan avatar nguyenhaiphan commented on June 15, 2024

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.

crynobone avatar crynobone commented on June 15, 2024

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.

nguyenhaiphan avatar nguyenhaiphan commented on June 15, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.