mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-09 20:52:56 +01:00
Multi-db support
This commit is contained in:
parent
d5a63ffeef
commit
aaba8e4ab1
@ -4,6 +4,7 @@ namespace App\Models;
|
||||
|
||||
use App;
|
||||
use App\Events\UserSettingsChanged;
|
||||
use App\Models\LookupAccount;
|
||||
use App\Models\Traits\GeneratesNumbers;
|
||||
use App\Models\Traits\PresentsInvoice;
|
||||
use App\Models\Traits\SendsEmails;
|
||||
@ -1655,6 +1656,12 @@ class Account extends Eloquent
|
||||
}
|
||||
}
|
||||
|
||||
Account::creating(function ($account)
|
||||
{
|
||||
LookupAccount::createAccount($account->account_key, $account->company_id);
|
||||
});
|
||||
|
||||
|
||||
Account::updated(function ($account) {
|
||||
// prevent firing event if the invoice/quote counter was changed
|
||||
// TODO: remove once counters are moved to separate table
|
||||
|
@ -22,4 +22,31 @@ class LookupAccount extends LookupModel
|
||||
return $this->belongsTo('App\Models\LookupCompany');
|
||||
}
|
||||
|
||||
public static function createAccount($accountKey, $companyId)
|
||||
{
|
||||
if (! env('MULTI_DB_ENABLED')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$current = config('database.default');
|
||||
config(['database.default' => DB_NINJA_LOOKUP]);
|
||||
|
||||
$server = DbServer::whereName($current)->firstOrFail();
|
||||
$lookupCompany = LookupCompany::whereDbServerId($server->id)
|
||||
->whereCompanyId($companyId)->first();
|
||||
|
||||
if (! $lookupCompany) {
|
||||
$lookupCompany = LookupCompany::create([
|
||||
'db_server_id' => $server->id,
|
||||
'company_id' => $companyId,
|
||||
]);
|
||||
}
|
||||
|
||||
LookupAccount::create([
|
||||
'lookup_company_id' => $lookupCompany->id,
|
||||
'account_key' => $accountKey,
|
||||
]);
|
||||
|
||||
static::setDbServer($current);
|
||||
}
|
||||
}
|
||||
|
@ -61,10 +61,11 @@ class LookupModel extends Eloquent
|
||||
config(['database.default' => DB_NINJA_LOOKUP]);
|
||||
|
||||
if ($lookupUser = static::where($field, '=', $value)->first()) {
|
||||
$entity = new $className();
|
||||
$server = $lookupUser->getDbServer();
|
||||
static::setDbServer($server);
|
||||
|
||||
$entity = new $className();
|
||||
// check entity is found on the server
|
||||
if (! $entity::where($field, '=', $value)->first()) {
|
||||
abort("Looked up {$className} not found: {$field} => {$value}");
|
||||
}
|
||||
|
@ -413,7 +413,7 @@ class User extends Authenticatable
|
||||
}
|
||||
}
|
||||
|
||||
User::creating(function ($user)
|
||||
User::created(function ($user)
|
||||
{
|
||||
LookupUser::createNew($user->account->account_key, [
|
||||
'email' => $user->email,
|
||||
|
@ -25,7 +25,7 @@ class AddMultipleDatabaseSupport extends Migration
|
||||
});
|
||||
|
||||
Schema::table('lookup_users', function ($table) {
|
||||
$table->string('email')->change()->unique();
|
||||
$table->string('email')->change()->nullable()->unique();
|
||||
$table->unsignedInteger('user_id')->index();
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user