From 2fcccc27d484b682bbb77f5174a7ec584e4c34bb Mon Sep 17 00:00:00 2001 From: izica Date: Wed, 29 Mar 2023 10:57:15 +0700 Subject: [PATCH 1/2] Table namespaces --- src/Coders/Model/Factory.php | 15 ++++++++++++++- src/Coders/Model/Model.php | 19 ++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/Coders/Model/Factory.php b/src/Coders/Model/Factory.php index ff1dc3f..10befc0 100644 --- a/src/Coders/Model/Factory.php +++ b/src/Coders/Model/Factory.php @@ -492,6 +492,17 @@ protected function body(Model $model) return $body; } + /** + * @param string $table + * @return array + */ + protected function namespaces($table) + { + return collect(explode('__', $table))->slice(0, -1)->map(function ($particle) { + return Str::studly($particle); + })->all(); + } + /** * @param \Reliese\Coders\Model\Model $model * @param array $custom @@ -500,7 +511,9 @@ protected function body(Model $model) */ protected function modelPath(Model $model, $custom = []) { - $modelsDirectory = $this->path(array_merge([$this->config($model->getBlueprint(), 'path')], $custom)); + $namespaces = $this->namespaces($model->getTable()); + + $modelsDirectory = $this->path(array_merge([$this->config($model->getBlueprint(), 'path')], $namespaces, $custom)); if (! $this->files->isDirectory($modelsDirectory)) { $this->files->makeDirectory($modelsDirectory, 0755, true); diff --git a/src/Coders/Model/Model.php b/src/Coders/Model/Model.php index e9d2d0a..7bcd5bd 100644 --- a/src/Coders/Model/Model.php +++ b/src/Coders/Model/Model.php @@ -455,7 +455,13 @@ public function withReferences($references) */ public function withNamespace($namespace) { - $this->namespace = $namespace; + $tableNamespaces = collect( + explode('__', $this->blueprint->table()) + )->slice(0, -1)->map(function ($particle) { + return Str::studly($particle); + })->join('\\'); + + $this->namespace = $namespace . '\\' . $tableNamespaces; return $this; } @@ -752,11 +758,22 @@ public function withRelationNameStrategy($relationNameStrategy) $this->relationNameStrategy = $relationNameStrategy; } + /** + * @param string $table + * @return string + */ + public function removeTableNamespaces($table) + { + return collect(explode('__', $table))->last(); + } + /** * @param string $table */ public function removeTablePrefix($table) { + $table = $this->removeTableNamespaces($table); + if (($this->shouldRemoveTablePrefix()) && (substr($table, 0, strlen($this->tablePrefix)) == $this->tablePrefix)) { $table = substr($table, strlen($this->tablePrefix)); } From b6e7a2dbc95acd82c33a6ffbd23991632f2f8554 Mon Sep 17 00:00:00 2001 From: izica Date: Wed, 29 Mar 2023 11:23:52 +0700 Subject: [PATCH 2/2] hotfix namespace in not nested models --- src/Coders/Model/Model.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Coders/Model/Model.php b/src/Coders/Model/Model.php index 7bcd5bd..47e57cf 100644 --- a/src/Coders/Model/Model.php +++ b/src/Coders/Model/Model.php @@ -455,13 +455,11 @@ public function withReferences($references) */ public function withNamespace($namespace) { - $tableNamespaces = collect( + $this->namespace = collect( explode('__', $this->blueprint->table()) )->slice(0, -1)->map(function ($particle) { return Str::studly($particle); - })->join('\\'); - - $this->namespace = $namespace . '\\' . $tableNamespaces; + })->prepend($namespace)->join('\\'); return $this; }