Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.x] Adds support for custom connections #3

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MIGRATIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ All customizable models can be configured with additional fillable, guarded, hid

Customize the model using the available static properties:

- `$useConnection`: The connection name to use.
- `$useCasts`: The casts attributes to merge.
- `$useFillable`: The fillable attributes to merge.
- `$useGuarded`: The guarded attributes to merge.
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ class MyModel extends Model

From there, the end-developer can customize the model using the available static properties:

- `$useTable`: A custom table name to use.
- `$useConnection`: The custom connection name to use.
- `$useTable`: The custom table name to use.
- `$useCasts`: The casts attributes to merge.
- `$useFillable`: The fillable attributes to merge.
- `$useGuarded`: The guarded attributes to merge.
Expand Down
9 changes: 9 additions & 0 deletions src/CustomizableModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
*/
trait CustomizableModel
{
/**
* The connection name to use with the model.
*
* @var string|null
*/
public static $useConnection;

/**
* The table name to use for this customizable model.
*
Expand Down Expand Up @@ -76,6 +83,8 @@ protected static function bootCustomizableModel(): void
*/
protected function initializeCustomizableModel(): void
{
$this->connection = static::$useConnection;

$this->table = static::$useTable;

$resolve = static function (Closure|array $value, $model): array {
Expand Down
20 changes: 17 additions & 3 deletions tests/CustomizableModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,34 @@ class CustomizableModelTest extends TestCase
{
protected function setUp(): void
{
TestModel::$useConnection = null;
TestModel::$useTable = null;
TestModel::$useCasts = [];
TestModel::$useFillable = [];
TestModel::$useGuarded = [];
TestModel::$useHidden = [];
TestModel::$useVisible = [];
TestModel::$useAppends = [];
}

#[Test]
public function uses_default_table_name(): void
public function uses_default_connection(): void
{
static::assertNull((new TestModel())->getConnectionName());
}

#[Test]
public function uses_custom_connection(): void
{
TestModel::$useTable = 'foo';
TestModel::$useConnection = 'foo';

static::assertSame('foo', (new TestModel())->getTable());
static::assertSame('foo', (new TestModel())->getConnectionName());
}

#[Test]
public function uses_default_table_name(): void
{
static::assertSame('test_models', (new TestModel())->getTable());
}

#[Test]
Expand Down
Loading