|
6 | 6 |
|
7 | 7 | trait DynamicModelBinding
|
8 | 8 | {
|
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 |
10 | 14 | {
|
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 | + } |
14 | 22 |
|
| 23 | + public function bindDynamically(): void |
| 24 | + { |
15 | 25 | // 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); |
18 | 32 | }
|
19 | 33 |
|
20 | 34 | // set the table for the dynamic model
|
21 |
| - $this->setTable($tableName); |
| 35 | + $this->setTable(self::$dynamicTableName); |
22 | 36 |
|
23 | 37 | // apply primary key, incrementing and key type
|
24 | 38 | $connection = Schema::getConnection();
|
25 | 39 |
|
26 |
| - $table = $connection->getDoctrineSchemaManager()->listTableDetails($tableName); |
| 40 | + $table = $connection->getDoctrineSchemaManager()->listTableDetails(self::$dynamicTableName); |
27 | 41 |
|
28 | 42 | if (! $primaryKey = $table->getPrimaryKey()) {
|
29 | 43 | throw DynamicModelException::primaryKeyDoesNotExist();
|
30 | 44 | }
|
31 | 45 |
|
32 | 46 | $primaryKeyName = $primaryKey->getColumns()[0];
|
33 |
| - $primaryColumn = $connection->getDoctrineColumn($tableName, $primaryKeyName); |
| 47 | + $primaryColumn = $connection->getDoctrineColumn(self::$dynamicTableName, $primaryKeyName); |
34 | 48 |
|
35 | 49 | $this->primaryKey = $primaryColumn->getName();
|
36 | 50 | $this->incrementing = $primaryColumn->getAutoincrement();
|
|
0 commit comments