Skip to content

Commit

Permalink
Merge pull request #15 from recca0120/feat/multiple-keys
Browse files Browse the repository at this point in the history
support awobaz/compoships
  • Loading branch information
recca0120 authored Nov 4, 2024
2 parents dbcfd94 + d32e7e7 commit 0184045
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 67 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"php-http/client-common": "^2.7"
},
"require-dev": {
"awobaz/compoships": "^2.3",
"doctrine/dbal": "^3.5",
"guzzlehttp/guzzle": "^7.5",
"mockery/mockery": "^1.5",
Expand Down
39 changes: 39 additions & 0 deletions database/migrations/2024_11_04_000014_create_tasks_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->foreignId('category_id');
});

Schema::create('tasks', function (Blueprint $table) {
$table->id();
$table->foreignId('team_id');
$table->foreignId('category_id');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('tasks');
Schema::table('users', function (Blueprint $table) {
$table->dropColumn(['category_id']);
});
}
};
47 changes: 30 additions & 17 deletions src/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

class Relation
{
private static array $relationMap = [
\Awobaz\Compoships\Database\Eloquent\Relations\BelongsTo::class => BelongsTo::class,
\Awobaz\Compoships\Database\Eloquent\Relations\HasOne::class => HasOne::class,
\Awobaz\Compoships\Database\Eloquent\Relations\HasMany::class => HasMany::class,
];
private array $attributes;

public function __construct(array $attributes)
Expand All @@ -20,7 +25,7 @@ public function __construct(array $attributes)

public function type(): string
{
return $this->attributes['type'];
return self::$relationMap[$this->attributes['type']] ?? $this->attributes['type'];
}

public function related(): string
Expand All @@ -33,34 +38,38 @@ public function parent(): string
return $this->attributes['parent'];
}

public function localKey(): string
public function localKeys(): array
{
return $this->attributes['local_key'];
return (array) $this->attributes['local_key'];
}

public function localTable(): string
{
return Helpers::getTableName($this->localKey());
return Helpers::getTableName($this->localKeys()[0]);
}

public function localColumn(): string
public function localColumns(): array
{
return Helpers::getColumnName($this->localKey());
return array_map(static function (string $column) {
return Helpers::getColumnName($column);
}, $this->localKeys());
}

public function foreignKey(): string
public function foreignKeys(): array
{
return $this->attributes['foreign_key'];
return (array) $this->attributes['foreign_key'];
}

public function foreignTable(): string
{
return Helpers::getTableName($this->foreignKey());
return Helpers::getTableName($this->foreignKeys()[0]);
}

public function foreignColumn(): string
public function foreignColumns(): array
{
return Helpers::getColumnName($this->foreignKey());
return array_map(static function (string $column) {
return Helpers::getColumnName($column);
}, $this->foreignKeys());
}

public function morphClass(): ?string
Expand Down Expand Up @@ -119,8 +128,8 @@ public function relatedRelation(): Relation
'type' => $reverseLookup[$type] ?? $type,
'related' => $this->parent(),
'parent' => $this->related(),
'local_key' => $this->foreignKey(),
'foreign_key' => $this->localKey(),
'local_key' => $this->foreignKeys(),
'foreign_key' => $this->localKeys(),
'pivot' => $this->attributes['pivot'] ?? null,
'morph_class' => $this->morphClass(),
'morph_type' => $this->morphType(),
Expand Down Expand Up @@ -149,15 +158,19 @@ public function sortByRelation(): int
*/
public function sortByKeys(): array
{
return [$this->type(), $this->localKey(), $this->foreignKey()];
return [$this->type(), $this->localKeys(), $this->foreignKeys()];
}

public function uniqueId(): string
{
$localKey = Helpers::getTableName($this->localKey());
$foreignKey = Helpers::getTableName($this->foreignKey());
$sortBy = [];
foreach ($this->localKeys() as $localKey) {
$sortBy[] = Helpers::getTableName($localKey);
}
foreach ($this->foreignKeys() as $foreignKey) {
$sortBy[] = Helpers::getTableName($foreignKey);
}

$sortBy = [$localKey, $foreignKey];
sort($sortBy);

return implode('::', $sortBy);
Expand Down
4 changes: 2 additions & 2 deletions src/Template/DDL.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ private function renderRelation(Relation $relation): string
return sprintf(
'ALTER TABLE %s ADD FOREIGN KEY (%s) REFERENCES %s (%s)',
$relation->localTable(),
$relation->localColumn(),
implode(', ', $relation->localColumns()),
$relation->foreignTable(),
$relation->foreignColumn()
implode(', ', $relation->foreignColumns())
);
}
}
5 changes: 3 additions & 2 deletions src/Template/Er.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ private function renderTable(Table $table): string
$primaryKeys = $table->getPrimaryKeys();
$indexes = $table
->getRelations()
->flatMap(fn (Relation $relation) => [$relation->localColumn(), $relation->morphColumn()])
->filter();
->flatMap(fn (Relation $relation) => [...$relation->localColumns(), $relation->morphColumn()])
->filter()
->unique();

return $table->getColumns()
->map(fn (ColumnSchema $column) => $this->renderColumn($column, $primaryKeys, $indexes))
Expand Down
16 changes: 16 additions & 0 deletions tests/Fixtures/Models/Task.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Recca0120\LaravelErd\Tests\Fixtures\Models;

use Awobaz\Compoships\Compoships;
use Illuminate\Database\Eloquent\Model;

class Task extends Model
{
use Compoships;

public function user()
{
return $this->belongsTo(User::class, ['team_id', 'category_id'], ['team_id', 'category_id']);
}
}
7 changes: 7 additions & 0 deletions tests/Fixtures/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Recca0120\LaravelErd\Tests\Fixtures\Models;

use Awobaz\Compoships\Compoships;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
Expand All @@ -13,6 +14,7 @@
class User extends Model
{
use HasRoles;
use Compoships;

protected $fillable = ['name', 'email', 'password'];

Expand Down Expand Up @@ -59,4 +61,9 @@ public function devices(): BelongsToMany
{
return $this->belongsToMany(Device::class, 'user_device');
}

public function tasks()
{
return $this->hasMany(Task::class, ['team_id', 'category_id'], ['team_id', 'category_id']);
}
}
Loading

0 comments on commit 0184045

Please sign in to comment.