diff --git a/config/config.php b/config/config.php index 6eff947..65128f6 100644 --- a/config/config.php +++ b/config/config.php @@ -64,6 +64,16 @@ */ 'use_column_types' => true, + /* + * If you want to ignore specific columns in specific tables you can specify them here. + * Use the dot notation to specify the table and the column : table.column + * This option only apply when 'use_db_schema' is set to true. + */ + 'ignore_columns' => [ + // 'users.lastname', + // 'posts.description', + ], + /* * These colors will be used in the table representation for each entity in * your graph. diff --git a/src/GraphBuilder.php b/src/GraphBuilder.php index 0f4730d..3932ca6 100644 --- a/src/GraphBuilder.php +++ b/src/GraphBuilder.php @@ -3,11 +3,11 @@ namespace BeyondCode\ErdGenerator; use Illuminate\Database\Eloquent\Relations\BelongsToMany; -use Illuminate\Support\Facades\Schema; use phpDocumentor\GraphViz\Graph; use Illuminate\Support\Collection; use phpDocumentor\GraphViz\Node; use \Illuminate\Database\Eloquent\Model as EloquentModel; +use Illuminate\Support\Facades\Schema; class GraphBuilder { @@ -55,13 +55,10 @@ public function generateStructuredTextRepresentation(Collection $models) : strin if (count($columns) > 0) { $output .= "#### Attributes:\n\n"; foreach ($columns as $column) { - if (is_object($column)) { - $name = $column->getName(); - $typeName = $column->getType()->getName(); - } else { - $name = $column['name'] ?? ''; - $typeName = $column['type_name'] ?? ''; - } + + $name = isset($column['name']) ? $column['name'] : ''; + $typeName = isset($column['type_name']) ? $column['type_name'] : ''; + $columnType = config('erd-generator.use_column_types') ? ' (' . $typeName . ')' : ''; $output .= "- `" . $name . "`" . $columnType . "\n"; } @@ -104,23 +101,19 @@ protected function getTableColumnsFromModel(EloquentModel $model) { try { - $table = $model->getConnection()->getTablePrefix() . $model->getTable(); - $schema = $model->getConnection()->getDoctrineSchemaManager($table); - $databasePlatform = $schema->getDatabasePlatform(); - $databasePlatform->registerDoctrineTypeMapping('enum', 'string'); - - $database = null; - - if (strpos($table, '.')) { - list($database, $table) = explode('.', $table); + $table = $model->getTable(); + $columns = Schema::getColumns($table); + if (config('erd-generator.ignore_columns')) { + $columns = collect($columns)->filter(function($column) use($table) { + if (isset($column['name'])){ + return !in_array($table.'.'.$column['name'],config('erd-generator.ignore_columns')); + } + return false; + }); } - return $schema->listTableColumns($table, $database); - } catch (\Throwable $e) { - } + return $columns; - try { - return Schema::getColumns($model->getTable()); } catch (\Throwable $e) { } @@ -136,18 +129,15 @@ protected function getModelLabel(EloquentModel $model, string $label) if (config('erd-generator.use_db_schema')) { $columns = $this->getTableColumnsFromModel($model); foreach ($columns as $column) { - if (is_object($column)) { - $name = $column->getName(); - $typeName = $column->getType()->getName(); - } else { // it's an array! - $name = $column['name'] ?? ''; - $typeName = $column['type_name'] ?? ''; - } - $label = $name; - if (config('erd-generator.use_column_types')) { - $label .= ' ('. $typeName .')'; + + if (isset($column['name'])) { + $label = $column['name']; + if (config('erd-generator.use_column_types') && isset($column['type'])) { + $label .= ' ('.$column['type'].')'; + } + $table .= '