Skip to content

fix: prevent schema relation infinite loop during CRUD generation #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
76 changes: 47 additions & 29 deletions src/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class ModelGenerator
/**
* ModelGenerator constructor.
*
* @param string $table
* @param string $properties
* @param string $modelNamespace
* @param string $table
* @param string $properties
* @param string $modelNamespace
*/
public function __construct(string $table, string $properties, string $modelNamespace)
{
Expand Down Expand Up @@ -86,7 +86,9 @@ private function _getTableRelations(): array

/**
* Extract the table name from a fully qualified table name (e.g., database.table).
* @param string $foreignTable
*
* @param string $foreignTable
*
* @return string
*/
protected function extractTableName(string $foreignTable): string
Expand Down Expand Up @@ -114,11 +116,11 @@ protected function getBelongsTo(): array
$foreignTable = $this->extractTableName($relation['foreign_table']);

$eloquent[] = [
'name' => 'belongsTo',
'name' => 'belongsTo',
'relation_name' => Str::camel(Str::singular($foreignTable)),
'class' => Str::studly(Str::singular($foreignTable)),
'foreign_key' => $relation['columns'][0],
'owner_key' => $relation['foreign_columns'][0],
'class' => Str::studly(Str::singular($foreignTable)),
'foreign_key' => $relation['columns'][0],
'owner_key' => $relation['foreign_columns'][0],
];
}

Expand All @@ -127,36 +129,52 @@ protected function getBelongsTo(): array

protected function getOtherRelations(): array
{
$tables = Schema::getTableListing();
$eloquent = [];
try {
$tables = Schema::getTableListing();
$eloquent = [];

foreach ($tables as $table) {
$relations = Schema::getForeignKeys($table);
$indexes = collect(Schema::getIndexes($table));
$startTime = microtime(true);
$timeout = 10;

foreach ($relations as $relation) {
if ($relation['foreign_table'] != $this->table) {
continue;
foreach ($tables as $table) {
// Check for timeout
if (microtime(true) - $startTime > $timeout) {
break;
}

if (count($relation['foreign_columns']) != 1 || count($relation['columns']) != 1) {
try {
$relations = Schema::getForeignKeys($table);
$indexes = collect(Schema::getIndexes($table));

foreach ($relations as $relation) {
if ($relation['foreign_table'] != $this->table) {
continue;
}
if (count($relation['foreign_columns']) != 1 || count($relation['columns']) != 1) {
continue;
}
$isUniqueColumn = $this->getUniqueIndex($indexes, $relation['columns'][0]);
$foreignTable = $this->extractTableName($table);
$eloquent[] = [
'name' => $isUniqueColumn ? 'hasOne' : 'hasMany',
'relation_name' => Str::camel($isUniqueColumn ? Str::singular($foreignTable) : Str::plural($foreignTable)),
'class' => Str::studly(Str::singular($foreignTable)),
'foreign_key' => $relation['foreign_columns'][0],
'owner_key' => $relation['columns'][0],
];
}
} catch (\Exception $e) {
\Log::error("Error processing table $table: ".$e->getMessage());
continue;
}
}

$isUniqueColumn = $this->getUniqueIndex($indexes, $relation['columns'][0]);
$foreignTable = $this->extractTableName($table);
return $eloquent;
} catch (\Exception $e) {
\Log::error('Error in getOtherRelations: '.$e->getMessage());

$eloquent[] = [
'name' => $isUniqueColumn ? 'hasOne' : 'hasMany',
'relation_name' => Str::camel($isUniqueColumn ? Str::singular($foreignTable) : Str::plural($foreignTable)),
'class' => Str::studly(Str::singular($foreignTable)),
'foreign_key' => $relation['foreign_columns'][0],
'owner_key' => $relation['columns'][0],
];
}
return [];
}

return $eloquent;
}

private function getUniqueIndex($indexes, $column): bool
Expand Down