Skip to content

Commit 561bc56

Browse files
Fix hook schema usage
1 parent 878d4e1 commit 561bc56

File tree

1 file changed

+10
-32
lines changed

1 file changed

+10
-32
lines changed

src/Hooks/TranslatableHook.php

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Barryvdh\LaravelIdeHelper\Contracts\ModelHookInterface;
1010
use Illuminate\Contracts\Config\Repository as Config;
1111
use Illuminate\Database\Eloquent\Model;
12+
use Illuminate\Support\Facades\Schema;
1213

1314
class TranslatableHook implements ModelHookInterface
1415
{
@@ -28,43 +29,29 @@ public function run(ModelsCommand $command, Model $model): void
2829
$modelTranslation = $command->getLaravel()->make($className);
2930

3031
$table = $modelTranslation->getConnection()->getTablePrefix() . $modelTranslation->getTable();
31-
$schema = $modelTranslation->getConnection()->getDoctrineSchemaManager();
32-
$databasePlatform = $schema->getDatabasePlatform();
33-
$databasePlatform->registerDoctrineTypeMapping('enum', 'string');
3432

35-
$platformName = $databasePlatform->getName();
36-
37-
/** @var Config $config */
38-
$config = $command->getLaravel()->make(Config::class);
39-
40-
$customTypes = $config->get("ide-helper.custom_db_types.{$platformName}", []);
41-
foreach ($customTypes as $yourTypeName => $doctrineTypeName) {
42-
$databasePlatform->registerDoctrineTypeMapping($yourTypeName, $doctrineTypeName);
43-
}
44-
45-
$database = null;
46-
if (strpos($table, '.')) {
47-
[$database, $table] = explode('.', $table);
48-
}
49-
50-
$columns = $schema->listTableColumns($table, $database);
33+
$columns = $modelTranslation->getConnection()->getSchemaBuilder()->getColumns($table);
5134

5235
if (! $columns) {
5336
return;
5437
}
5538

5639
foreach ($columns as $column) {
57-
$name = $column->getName();
40+
$name = $column['name'];
5841

5942
if (! in_array($name, $model->translatedAttributes)) {
6043
continue;
6144
}
6245

46+
// Handle dates
6347
if (in_array($name, $modelTranslation->getDates())) {
6448
$type = $this->getDateClass();
6549
} else {
66-
$type = $column->getType()->getName();
50+
$type = $column['type_name'];
51+
52+
// Map the column types to PHP types
6753
switch ($type) {
54+
case 'varchar':
6855
case 'string':
6956
case 'text':
7057
case 'date':
@@ -100,23 +87,14 @@ public function run(ModelsCommand $command, Model $model): void
10087
}
10188
}
10289

103-
$comment = $column->getComment();
10490
$command->setProperty(
10591
$name,
10692
$type,
10793
true,
10894
true,
109-
$comment,
110-
true,
95+
null,
96+
$column['nullable'],
11197
);
11298
}
11399
}
114-
115-
116-
protected function getDateClass(): string
117-
{
118-
return class_exists(\Illuminate\Support\Facades\Date::class)
119-
? '\\' . get_class(\Illuminate\Support\Facades\Date::now())
120-
: '\Illuminate\Support\Carbon';
121-
}
122100
}

0 commit comments

Comments
 (0)