douyuanqia665858 2019-03-14 14:43
浏览 24

流明中的多租户

I want to make application with one "central" database and multiple "customers" databases (1 customer has 1 DB). I want to use lumen rest api framework. Let me explain logic behind databases and my issue.

Central DB (simplified):

Table: tenants

Columns: id(PK), id_tenant(FK), db_host, db_username, db_password, db_port, db_name

Customer DB (simplified):

Table: issues

Columns: id(PK), id_creator, issue_name, write_time

Lumen part

I have two models: Tenants

<?php

namespace App;

use Illuminate\Auth\Authenticatable;
use Laravel\Lumen\Auth\Authorizable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;

class Tenants extends Model implements AuthenticatableContract, AuthorizableContract
{
    use Authenticatable, Authorizable;
    protected $connection = 'mysql2';
    protected $fillable = [
        'id_tenant'
    ];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        
    ];
}

Issues:

<?php

namespace App;

use Illuminate\Auth\Authenticatable;
use Laravel\Lumen\Auth\Authorizable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;


class Issues extends Model implements AuthenticatableContract, AuthorizableContract
{
    use Authenticatable, Authorizable;

    protected $connection = 'tenant';

    protected $fillable = [
        'id_issue'
    ];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        
    ];
}

Also, I have created file in config/database.php:

<?php
return [

    'default' => 'mysql',
    'connections' => [       
        'mysql2' => [
            'driver'    => 'mysql',
            'host'      => 'dbhost',
            'database'  => 'dbName',
            'username'  => 'dbUser',
            'password'  => 'secretPass',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],
        'tenant' => [
            'driver' => 'mysql',
            'host' => '',
            'port' => '',
            'database' => '',
            'username' => '',
            'password' => '',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ]
    ],
  ]

  ?>

I have written 2 controllers (TenantsController and IssuesController), which are working with no issues, when i hardcode info about tenant database in config/database.php.

My issue is:

When I call route: api/tenant/1 (uses => TenantsController@getTenantById) Lumen returns correct JSON with db data of requested tenant with id 1. This data I need to fill connection variables to another (customer) database, where table issues is located. How it is possible to set database connection 'tenant' to call route api/issues from second DB and get list of all issues??

Note: Let's abstract from user authentication and secure API.

Thank you for help in advance.

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 下图接收小电路,谁知道原理
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测