Skip to content

Commit

Permalink
Add Embedded prefix (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
msmakouz authored Jan 11, 2022
1 parent ccc3ab2 commit 4838734
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/Generator/GenerateRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ final class GenerateRelations implements GeneratorInterface
'fkOnDelete' => RelationSchema::FK_ON_DELETE,
'indexCreate' => RelationSchema::INDEX_CREATE,
'morphKeyLength' => RelationSchema::MORPH_KEY_LENGTH,
'embeddedPrefix' => RelationSchema::EMBEDDED_PREFIX,

// deprecated
'though' => Relation::THROUGH_ENTITY,
Expand Down
9 changes: 8 additions & 1 deletion src/Relation/Embedded.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ final class Embedded extends RelationSchema
// relation schema options
protected const RELATION_SCHEMA = [
Relation::LOAD => Relation::LOAD_EAGER,
self::EMBEDDED_PREFIX => '',
];

/**
Expand All @@ -31,7 +32,7 @@ public function compute(Registry $registry): void

// each embedded entity must isolated
$target = clone $target;
$target->setRole($source->getRole() . ':' . $target->getRole());
$target->setRole($source->getRole() . ':' . $target->getRole() . ':' . $this->name);

// embedded entity must point to the same table as parent entity
$registry->register($target);
Expand All @@ -40,6 +41,12 @@ public function compute(Registry $registry): void
// isolated
$this->target = $target->getRole();

$prefix = $this->getOptions()->get(self::EMBEDDED_PREFIX);
assert(\is_string($prefix));
foreach ($target->getFields() as $field) {
$field->setColumn($prefix . $field->getColumn());
}

foreach ($source->getFields() as $name => $field) {
if ($field->isPrimary()) {
// sync primary keys
Expand Down
9 changes: 8 additions & 1 deletion src/Relation/RelationSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,16 @@ abstract class RelationSchema implements RelationInterface
public const FK_ON_DELETE = 1004;
public const INVERSE = 1005;
public const MORPH_KEY_LENGTH = 1009;
public const EMBEDDED_PREFIX = 1010;

// options to be excluded from generated schema (helpers)
protected const EXCLUDE = [self::FK_CREATE, self::FK_ACTION, self::FK_ON_DELETE, self::INDEX_CREATE];
protected const EXCLUDE = [
self::FK_CREATE,
self::FK_ACTION,
self::FK_ON_DELETE,
self::INDEX_CREATE,
self::EMBEDDED_PREFIX,
];

// exported relation type
protected const RELATION_TYPE = null;
Expand Down
59 changes: 41 additions & 18 deletions tests/Schema/Relation/EmbeddedTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Cycle\Schema\Tests\Relation;
Expand Down Expand Up @@ -98,15 +91,15 @@ public function testPackSchema(): void
$schema['composite'][Schema::RELATIONS]['embedded'][\Cycle\ORM\Relation::LOAD]
);

$this->assertArrayHasKey('composite:embedded', $schema);
$this->assertSame(['p_id'], $schema['composite:embedded'][Schema::PRIMARY_KEY]);
$this->assertSame('default', $schema['composite:embedded'][Schema::DATABASE]);
$this->assertSame('composite', $schema['composite:embedded'][Schema::TABLE]);
$this->assertArrayHasKey('composite:embedded:embedded', $schema);
$this->assertSame(['p_id'], $schema['composite:embedded:embedded'][Schema::PRIMARY_KEY]);
$this->assertSame('default', $schema['composite:embedded:embedded'][Schema::DATABASE]);
$this->assertSame('composite', $schema['composite:embedded:embedded'][Schema::TABLE]);

$this->assertSame([
'p_embedded' => 'embedded_column',
'p_id' => 'id',
], $schema['composite:embedded'][Schema::COLUMNS]);
], $schema['composite:embedded:embedded'][Schema::COLUMNS]);
}

public function testPackSchemaLazyLoad(): void
Expand Down Expand Up @@ -142,15 +135,15 @@ public function testPackSchemaLazyLoad(): void
$schema['composite'][Schema::RELATIONS]['embedded'][\Cycle\ORM\Relation::LOAD]
);

$this->assertArrayHasKey('composite:embedded', $schema);
$this->assertSame(['p_id'], $schema['composite:embedded'][Schema::PRIMARY_KEY]);
$this->assertSame('default', $schema['composite:embedded'][Schema::DATABASE]);
$this->assertSame('composite', $schema['composite:embedded'][Schema::TABLE]);
$this->assertArrayHasKey('composite:embedded:embedded', $schema);
$this->assertSame(['p_id'], $schema['composite:embedded:embedded'][Schema::PRIMARY_KEY]);
$this->assertSame('default', $schema['composite:embedded:embedded'][Schema::DATABASE]);
$this->assertSame('composite', $schema['composite:embedded:embedded'][Schema::TABLE]);

$this->assertSame([
'p_embedded' => 'embedded_column',
'p_id' => 'id',
], $schema['composite:embedded'][Schema::COLUMNS]);
], $schema['composite:embedded:embedded'][Schema::COLUMNS]);
}

public function testRenderTable(): void
Expand Down Expand Up @@ -201,7 +194,7 @@ public function testEmbedIdFieldWithPrefix(): void
$r->register($e);

$this->expectException(EmbeddedPrimaryKeyException::class);
$this->expectExceptionMessage('Entity `composite:embedded` has conflicted field `p_id`.');
$this->expectExceptionMessage('Entity `composite:embedded:embedded` has conflicted field `p_id`.');

(new Compiler())->compile(
$r,
Expand All @@ -212,4 +205,34 @@ public function testEmbedIdFieldWithPrefix(): void
]
);
}

public function testEmbeddedPrefix(): void
{
$c = Composite::define();
$e = EmbeddedEntity::define();

$c->getRelations()->set(
'embedded',
(new Relation())->setTarget('embedded')->setType('embedded')
);

$c->getRelations()->get('embedded')->getOptions()->set('embeddedPrefix', 'prefix_');

$r = new Registry($this->dbal);
$r->register($c)->linkTable($c, 'default', 'composite');
$r->register($e);

(new Compiler())->compile($r, [
new GenerateRelations(['embedded' => new Embedded()]),
$t = new RenderTables(),
new RenderRelations(),
]);

$t->getReflector()->run();

$table = $this->getDriver()->getSchema('composite');

$this->assertTrue($table->hasColumn('id'));
$this->assertTrue($table->hasColumn('prefix_embedded_column'));
}
}

0 comments on commit 4838734

Please sign in to comment.