Skip to content

Commit

Permalink
New alias with eval
Browse files Browse the repository at this point in the history
  • Loading branch information
railken committed Apr 5, 2024
1 parent 79d74bf commit 19d01f1
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/DataSchema/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,24 @@ public function reloadByDataSchema(DataSchema $dataSchema)

public function newEntity(array $parameters = [])
{
$model = new Model($parameters);
$aliasName = ucfirst($this->dataSchema->name);
$namespace = "App\Models\DataSchema";
$fullAliasName = "\\".$namespace."\\".$aliasName;

// Possibility to write your own model
if (!class_exists($fullAliasName)) {
$this->newAlias($namespace, Model::class, $aliasName);
}

$model = new $fullAliasName($parameters);
$model->setTable(Helper::toTable($this->dataSchema->name));
$model->setManager($this);
$model->ini(null, true);

return $model;
}

public function newAlias($namespace, $original, $alias) {
eval("namespace $namespace;\n class $alias extends \\$original {}");
}
}

0 comments on commit 19d01f1

Please sign in to comment.