Skip to content
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

Table namespaces #256

Open
wants to merge 2 commits into
base: v1.x
Choose a base branch
from
Open
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
15 changes: 14 additions & 1 deletion src/Coders/Model/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
17 changes: 16 additions & 1 deletion src/Coders/Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,11 @@ public function withReferences($references)
*/
public function withNamespace($namespace)
{
$this->namespace = $namespace;
$this->namespace = collect(
explode('__', $this->blueprint->table())
)->slice(0, -1)->map(function ($particle) {
return Str::studly($particle);
})->prepend($namespace)->join('\\');

return $this;
}
Expand Down Expand Up @@ -752,11 +756,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));
}
Expand Down