Skip to content

Commit 3865ec7

Browse files
author
Ronald A Richardson
committed
fix: remove Model/Setting constructors that broke sandbox connection switching
The constructors introduced in both Model.php and Setting.php called: $this->connection = config('fleetbase.db.connection'); This config key does not exist (correct key: fleetbase.connection.db), so config() returned null, overwriting the child class's declared $connection property on every instantiation. A subsequent attempt to guard with empty($this->connection) overcorrected in the opposite direction: models without an explicit $connection declaration (the sandbox-aware ones) were always resolved to 'mysql', ignoring the database.default switch that sandbox mode sets to 'sandbox'. The correct fix is simply to remove both constructors entirely: - PHP initialises class property declarations before any constructor runs, so models that explicitly declare $connection = 'mysql' (Invite, Setting, User, Company, Role, Permission, etc.) will always use that value — no constructor logic needed. - Models that do NOT declare $connection (ApiCredential, CompanyUser, Activity, etc.) correctly inherit null from Eloquent's base class, which causes Laravel to resolve the connection from config('database.default'). In sandbox mode that is 'sandbox'; in normal mode it is 'mysql'. This is exactly the intended behaviour. No functional change for any model — this purely restores the correct connection resolution that existed before the constructors were added.
1 parent 561d3cd commit 3865ec7

2 files changed

Lines changed: 0 additions & 50 deletions

File tree

src/Models/Model.php

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,35 +30,6 @@ 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-
// Resolve the configured production DB connection name.
45-
// The correct config key is `fleetbase.connection.db`; the previously
46-
// used key `fleetbase.db.connection` was a typo that returned null,
47-
// causing every model to silently fall back to `database.default`.
48-
// When sandbox mode is active `database.default` is switched to
49-
// `sandbox`, which meant models with an explicit `$connection = 'mysql'`
50-
// (e.g. Invite) were still written to the sandbox database because the
51-
// parent constructor overwrote the child's property with null.
52-
//
53-
// Only apply the override when the child class has NOT declared its own
54-
// explicit $connection, so models that intentionally pin themselves to
55-
// a specific connection (e.g. `protected $connection = 'mysql'`) are
56-
// left untouched.
57-
if (empty($this->connection)) {
58-
$this->connection = config('fleetbase.connection.db', config('database.connections.mysql') ? 'mysql' : config('database.default'));
59-
}
60-
}
61-
6233
/**
6334
* The primary key for the model.
6435
*

src/Models/Setting.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,6 @@ 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-
// Settings must always read/write from the production database.
35-
// Do not overwrite the explicit $connection = 'mysql' declared below;
36-
// the previously used config key `fleetbase.db.connection` was a typo
37-
// (correct key: `fleetbase.connection.db`) that returned null and caused
38-
// this model to silently follow `database.default` into the sandbox DB.
39-
if (empty($this->connection)) {
40-
$this->connection = config('fleetbase.connection.db', 'mysql');
41-
}
42-
}
43-
4423
/**
4524
* No timestamp columns.
4625
*

0 commit comments

Comments
 (0)