Skip to content

Commit e1814fc

Browse files
author
Ronald A Richardson
committed
fix: correct config key typo and pin Invite to production DB via getConnectionName()
Reverts the two overcorrecting commits (561d3cd, 3865ec7) and replaces them with the minimal, correct changes: ## 1. Config key typo fix (Model.php, Setting.php) The base Model constructor and Setting constructor both used: $this->connection = config('fleetbase.db.connection'); The correct key is 'fleetbase.connection.db'. The wrong key returned null, which caused $this->connection to be set to null on every instantiation, making all models fall back to config('database.default'). In sandbox mode that is 'sandbox', so models that explicitly declared $connection = 'mysql' (Invite, Setting, User, Company, etc.) were silently overwritten. Fixed to: config('fleetbase.connection.db', 'mysql') The constructor is intentionally kept — it is the correct place to resolve the configured production connection name for models that do not declare their own $connection. ## 2. Invite::getConnectionName() override (Invite.php) Invites must always be stored in and read from the production database, regardless of sandbox mode. The $connection property alone is insufficient because the base Model constructor overwrites it at instantiation time. Overriding getConnectionName() is the Laravel-idiomatic way to enforce a hard connection binding at the model level — it is called by Eloquent for every query and cannot be overridden by runtime config changes.
1 parent 3865ec7 commit e1814fc

3 files changed

Lines changed: 47 additions & 0 deletions

File tree

src/Models/Invite.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,25 @@ class Invite extends Model
3838
*/
3939
protected $connection = 'mysql';
4040

41+
/**
42+
* Invites must always be stored in the production database.
43+
*
44+
* Sandbox mode switches `database.default` to `sandbox` and the base
45+
* Model constructor sets `$this->connection` from config, which means
46+
* any model without an explicit override would write to the sandbox DB.
47+
* Overriding getConnectionName() here ensures that no matter what the
48+
* runtime config state is, Invite records are always read from and
49+
* written to the production connection. This is intentional: invite
50+
* codes must be resolvable when the invited user accepts the link,
51+
* which always happens outside of sandbox context.
52+
*
53+
* @return string
54+
*/
55+
public function getConnectionName(): string
56+
{
57+
return config('fleetbase.connection.db', 'mysql');
58+
}
59+
4160
/**
4261
* These attributes that can be queried.
4362
*

src/Models/Model.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ class Model extends EloquentModel
3030
public const UUID_COLUMN = 'uuid';
3131
public const PUBLIC_ID_COLUMN = 'public_id';
3232

33+
/**
34+
* Create a new instance of the model.
35+
*
36+
* @param array $attributes the attributes to set on the model
37+
*
38+
* @return void
39+
*/
40+
public function __construct(array $attributes = [])
41+
{
42+
parent::__construct($attributes);
43+
44+
$this->connection = config('fleetbase.connection.db', 'mysql');
45+
}
46+
3347
/**
3448
* The primary key for the model.
3549
*

src/Models/Setting.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@ class Setting extends EloquentModel
2020
use Searchable;
2121
use Filterable;
2222

23+
/**
24+
* Create a new instance of the model.
25+
*
26+
* @param array $attributes the attributes to set on the model
27+
*
28+
* @return void
29+
*/
30+
public function __construct(array $attributes = [])
31+
{
32+
parent::__construct($attributes);
33+
34+
$this->connection = config('fleetbase.connection.db', 'mysql');
35+
}
36+
2337
/**
2438
* No timestamp columns.
2539
*

0 commit comments

Comments
 (0)