Skip to content
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
10 changes: 10 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
56 changes: 23 additions & 33 deletions src/GraphBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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";
}
Expand Down Expand Up @@ -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) {
}

Expand All @@ -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 .= '<tr width="100%"><td port="' . $column['name'] . '" align="left" width="100%" bgcolor="'.config('erd-generator.table.row_background_color').'"><font color="'.config('erd-generator.table.row_font_color').'" >' . $label . '</font></td></tr>' . PHP_EOL;
}
$table .= '<tr width="100%"><td port="' . $name . '" align="left" width="100%" bgcolor="'.config('erd-generator.table.row_background_color').'"><font color="'.config('erd-generator.table.row_font_color').'" >' . $label . '</font></td></tr>' . PHP_EOL;

}
}

Expand Down
17 changes: 17 additions & 0 deletions tests/GenerationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ public function it_generated_graphviz_for_test_models_with_db_columns()
$this->assertMatchesSnapshot(Artisan::output());
}

#[Test]
public function it_generated_graphviz_for_test_models_with_db_columns_with_some_excluded_on_config()
{
$this->app['config']->set('erd-generator.use_column_types', false);
$this->app['config']->set('erd-generator.directories', [__DIR__ . '/Models']);
$this->app['config']->set('erd-generator.ignore_columns', [
'users.email',
'posts.body'
]);

Artisan::call('generate:erd', [
'--format' => 'text'
]);

$this->assertMatchesSnapshot(Artisan::output());
}

#[Test]
public function it_generated_graphviz_for_test_models_with_aliases()
{
Expand Down
Loading