Skip to content

Commit 943edff

Browse files
committed
Call bindDynamically in __constructor
1 parent 63bb6ba commit 943edff

7 files changed

+67
-20
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ you can just create your own Eloquent model and **extend** it by the **DynamicMo
113113
or if the model is already extended by another model, you can just use the **DynamicModelBinding** trait.
114114

115115
If you use the trait, make sure your model implements the **DynamicModelInterface**.
116-
Also make sure to resolve these models through the **DynamicModelFactory**!
116+
Also make sure to resolve these models through the **DynamicModelFactory** and
117+
call the `bindDynamically` function in the `__constructor`!
117118

118119
``` php
119120
namespace App\Models;
@@ -133,11 +134,21 @@ class MyDynamicModel extends DynamicModel
133134
}
134135
}
135136

136-
# option 2: use the trait
137+
# option 2: use the trait when class already extends a class
137138
class MyDynamicModel extends SomeBaseModel implements DynamicModelInterface
138139
{
139140
use DynamicModelBinding;
140141

142+
protected $guarded = [];
143+
144+
// !!important!! call the parent constructor and bindDynamically function in the constructor!
145+
public function __construct(array $attributes = [])
146+
{
147+
parent::__construct($attributes);
148+
149+
$this->bindDynamically();
150+
}
151+
141152
public function doSomething()
142153
{
143154
// do something

src/DynamicModel.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,11 @@ class DynamicModel extends Model implements DynamicModelInterface
1414
* and implementing the DynamicModelInterface!
1515
*/
1616
protected $guarded = [];
17+
18+
public function __construct(array $attributes = [])
19+
{
20+
parent::__construct($attributes);
21+
22+
$this->bindDynamically();
23+
}
1724
}

src/DynamicModelBinding.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,45 @@
66

77
trait DynamicModelBinding
88
{
9-
public function bindDynamically(string $tableName, string $dbConnection = null): void
9+
private static string $dynamicTableName;
10+
11+
private static ?string $dynamicDBConnection = null;
12+
13+
public static function setDynamicTableName(string $tableName): void
1014
{
11-
if (! Schema::hasTable($tableName)) {
12-
throw DynamicModelException::tableDoesNotExist($tableName);
13-
}
15+
self::$dynamicTableName = $tableName;
16+
}
17+
18+
public static function setDynamicDBConnection(string $dbConnection): void
19+
{
20+
self::$dynamicDBConnection = $dbConnection;
21+
}
1422

23+
public function bindDynamically(): void
24+
{
1525
// change connection, if desired
16-
if ($dbConnection) {
17-
$this->setConnection($dbConnection);
26+
if (self::$dynamicDBConnection) {
27+
$this->setConnection(self::$dynamicDBConnection);
28+
}
29+
30+
if (! Schema::hasTable(self::$dynamicTableName)) {
31+
throw DynamicModelException::tableDoesNotExist(self::$dynamicTableName);
1832
}
1933

2034
// set the table for the dynamic model
21-
$this->setTable($tableName);
35+
$this->setTable(self::$dynamicTableName);
2236

2337
// apply primary key, incrementing and key type
2438
$connection = Schema::getConnection();
2539

26-
$table = $connection->getDoctrineSchemaManager()->listTableDetails($tableName);
40+
$table = $connection->getDoctrineSchemaManager()->listTableDetails(self::$dynamicTableName);
2741

2842
if (! $primaryKey = $table->getPrimaryKey()) {
2943
throw DynamicModelException::primaryKeyDoesNotExist();
3044
}
3145

3246
$primaryKeyName = $primaryKey->getColumns()[0];
33-
$primaryColumn = $connection->getDoctrineColumn($tableName, $primaryKeyName);
47+
$primaryColumn = $connection->getDoctrineColumn(self::$dynamicTableName, $primaryKeyName);
3448

3549
$this->primaryKey = $primaryColumn->getName();
3650
$this->incrementing = $primaryColumn->getAutoincrement();

src/DynamicModelFactory.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ class DynamicModelFactory
1212
*/
1313
public function create(string $concreteClassName, string $tableName, string $dbConnection = null): Model&DynamicModelInterface
1414
{
15-
$dynamicModel = new $concreteClassName();
16-
17-
// tell the IDE which type $dynamicModel should be...
18-
/** @var $dynamicModel Model&DynamicModelInterface */
19-
if (! method_exists($dynamicModel, 'bindDynamically')) {
15+
/** @var $concreteClassName Model&DynamicModelInterface */
16+
if (! method_exists($concreteClassName, 'bindDynamically')) {
2017
throw DynamicModelException::bindFuncDoesNotExist($concreteClassName);
2118
}
2219

23-
$dynamicModel->bindDynamically($tableName, $dbConnection);
20+
$concreteClassName::setDynamicTableName($tableName);
21+
if ($dbConnection) {
22+
$concreteClassName::setDynamicDBConnection($dbConnection);
23+
}
2424

25-
return $dynamicModel;
25+
return new $concreteClassName();
2626
}
2727
}

src/DynamicModelInterface.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44

55
interface DynamicModelInterface
66
{
7+
public static function setDynamicTableName(string $tableName): void;
8+
9+
public static function setDynamicDBConnection(string $dbConnection): void;
10+
711
/**
812
* Make sure the DynamicModel has a bindDynamic function.
913
*/
10-
public function bindDynamically(string $tableName, string $dbConnection = null): void;
14+
public function bindDynamically(): void;
1115
}

src/LaravelDynamicModelServiceProvider.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ public function register()
3131
}
3232

3333
return app(DynamicModelFactory::class)
34-
->create(DynamicModel::class, $parameters['table_name']);
34+
->create(
35+
DynamicModel::class,
36+
$parameters['table_name'],
37+
$parameters['db_connection'] ?? null
38+
);
3539
});
3640
}
3741
}

tests/MyTraitedDynamicModel.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,11 @@ class MyTraitedDynamicModel extends MyBaseModel implements DynamicModelInterface
1010
use DynamicModelBinding;
1111

1212
protected $guarded = [];
13+
14+
public function __construct(array $attributes = [])
15+
{
16+
parent::__construct($attributes);
17+
18+
$this->bindDynamically();
19+
}
1320
}

0 commit comments

Comments
 (0)