Skip to content

Commit 0277242

Browse files
committed
Refactor generateStructuredTextRepresentation to handle column details more robustly, accommodating both object and array formats for column data.
1 parent 1572c0e commit 0277242

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

src/GraphBuilder.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,63 +33,70 @@ public function buildGraph(Collection $models) : Graph
3333

3434
/**
3535
* Generate a structured text representation of the ER diagram
36-
*
36+
*
3737
* @param Collection $models
3838
* @return string
3939
*/
4040
public function generateStructuredTextRepresentation(Collection $models) : string
4141
{
4242
$output = "# Entity Relationship Diagram\n\n";
43-
43+
4444
// First list all models/entities with their attributes
4545
$output .= "## Entities\n\n";
46-
46+
4747
foreach ($models as $model) {
4848
/** @var Model $model */
4949
$eloquentModel = app($model->getModel());
5050
$output .= "### " . $model->getLabel() . " (`" . $model->getModel() . "`)\n\n";
51-
51+
5252
// Add table columns if available
5353
if (config('erd-generator.use_db_schema')) {
5454
$columns = $this->getTableColumnsFromModel($eloquentModel);
5555
if (count($columns) > 0) {
5656
$output .= "#### Attributes:\n\n";
5757
foreach ($columns as $column) {
58-
$columnType = config('erd-generator.use_column_types') ? ' (' . $column->getType()->getName() . ')' : '';
59-
$output .= "- `" . $column->getName() . "`" . $columnType . "\n";
58+
if (is_object($column)) {
59+
$name = $column->getName();
60+
$typeName = $column->getType()->getName();
61+
} else {
62+
$name = $column['name'] ?? '';
63+
$typeName = $column['type_name'] ?? '';
64+
}
65+
$columnType = config('erd-generator.use_column_types') ? ' (' . $typeName . ')' : '';
66+
$output .= "- `" . $name . "`" . $columnType . "\n";
6067
}
6168
$output .= "\n";
6269
}
6370
}
6471
}
65-
72+
6673
// Then list all relationships
6774
$output .= "## Relationships\n\n";
68-
75+
6976
foreach ($models as $model) {
7077
/** @var Model $model */
7178
if (count($model->getRelations()) > 0) {
7279
$output .= "### " . $model->getLabel() . " Relationships\n\n";
73-
80+
7481
foreach ($model->getRelations() as $relation) {
7582
/** @var ModelRelation $relation */
7683
// Find the related model by comparing model class names
7784
$relatedModelClass = $relation->getModel();
85+
7886
$relatedModel = $models->first(function ($m) use ($relatedModelClass) {
7987
return $m->getModel() === $relatedModelClass;
8088
});
81-
8289
if ($relatedModel) {
83-
$output .= "- **" . $relation->getType() . "** `" . $relation->getName() . "` to " .
84-
$relatedModel->getLabel() . " (Local Key: `" . $relation->getLocalKey() .
90+
$output .= "- **" . $relation->getType() . "** `" . $relation->getName() . "` to " .
91+
$relatedModel->getLabel() . " (Local Key: `" . $relation->getLocalKey() .
8592
"`, Foreign Key: `" . $relation->getForeignKey() . "`)\n";
8693
}
8794
}
88-
95+
8996
$output .= "\n";
9097
}
9198
}
92-
99+
93100
return $output;
94101
}
95102

0 commit comments

Comments
 (0)