Skip to content

Commit

Permalink
Merge pull request #88 from cycle/bugfix/table-inheritance
Browse files Browse the repository at this point in the history
Improving table and database name searching
  • Loading branch information
roxblnfk authored Sep 15, 2023
2 parents d3f62a7 + 2c64506 commit abd3c24
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 11 deletions.
48 changes: 37 additions & 11 deletions src/TableInheritance.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Cycle\Annotated\Annotation\Inheritance;
use Cycle\Annotated\Exception\AnnotationException;
use Cycle\Annotated\Utils\EntityUtils;
use Cycle\Schema\Definition\Entity;
use Cycle\Schema\Definition\Entity as EntitySchema;
use Cycle\Schema\Definition\Inheritance\JoinedTable as JoinedTableInheritanceSchema;
use Cycle\Schema\Definition\Inheritance\SingleTable as SingleTableInheritanceSchema;
Expand Down Expand Up @@ -57,8 +58,8 @@ public function run(Registry $registry): Registry
$childClass = $parent->getClass();
} while ($this->parseMetadata($parent, Inheritance::class) !== null);

if ($entity = $this->initInheritance($annotation, $child, $parent)) {
$found[] = $entity;
if ($inheritanceEntity = $this->initInheritance($annotation, $child, $parent)) {
$found[] = $inheritanceEntity;
}
}

Expand All @@ -67,17 +68,10 @@ public function run(Registry $registry): Registry
if (!$registry->hasEntity($child->getRole())) {
$registry->register($child);

$database = $child->getDatabase();
$tableName = $child->getTableName();
if ($entity->getInheritance() instanceof SingleTableInheritanceSchema) {
$database = $parent->getDatabase();
$tableName = $parent->getTableName();
}

$registry->linkTable(
$child,
$database,
$tableName,
$this->getDatabase($child, $registry),
$this->getTableName($child, $registry)
);
}
}
Expand Down Expand Up @@ -285,4 +279,36 @@ private function getOuterFields(

return $parent->getPrimaryFields();
}

private function getTableName(Entity $child, Registry $registry): string
{
$parent = $this->findParent($registry, $this->utils->findParent($child->getClass(), false));

$inheritance = $parent->getInheritance();
if (!$inheritance instanceof SingleTableInheritanceSchema) {
return $child->getTableName();
}
$entities = \array_map(
static fn (string $role) => $registry->getEntity($role)->getClass(),
$inheritance->getChildren()
);

return \in_array($child->getClass(), $entities, true) ? $parent->getTableName() : $child->getTableName();
}

private function getDatabase(Entity $child, Registry $registry): ?string
{
$parent = $this->findParent($registry, $this->utils->findParent($child->getClass(), false));

$inheritance = $parent->getInheritance();
if (!$inheritance instanceof SingleTableInheritanceSchema) {
return $child->getDatabase();
}
$entities = \array_map(
static fn (string $role) => $registry->getEntity($role)->getClass(),
$inheritance->getChildren()
);

return \in_array($child->getClass(), $entities, true) ? $parent->getDatabase() : $child->getDatabase();
}
}
36 changes: 36 additions & 0 deletions tests/Annotated/Functional/Driver/Common/InheritanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Cycle\Annotated\Tests\Fixtures\Fixtures16\Ceo;
use Cycle\Annotated\Tests\Fixtures\Fixtures16\Customer;
use Cycle\Annotated\Tests\Fixtures\Fixtures16\Employee;
use Cycle\Annotated\Tests\Fixtures\Fixtures16\Executive;
use Cycle\Annotated\Tests\Fixtures\Fixtures16\Person;
use Cycle\ORM\SchemaInterface;
use Cycle\Schema\Compiler;
use Cycle\Schema\Generator\GenerateRelations;
Expand All @@ -21,7 +23,9 @@
use Cycle\Schema\Generator\ResetTables;
use Cycle\Schema\Generator\SyncTables;
use Cycle\Schema\Registry;
use Spiral\Attributes\AttributeReader;
use Spiral\Attributes\ReaderInterface;
use Spiral\Tokenizer\ClassesInterface;
use Spiral\Tokenizer\Config\TokenizerConfig;
use Spiral\Tokenizer\Tokenizer;

Expand Down Expand Up @@ -128,4 +132,36 @@ public function testTableInheritance(ReaderInterface $reader): void
'hidden' => 'hidden',
], $schema['beaver'][SchemaInterface::COLUMNS]);
}

public function testTableInheritanceWithIncorrectClassesOrder(): void
{
$r = new Registry($this->dbal);
$reader = new AttributeReader();
$locator = $this->createMock(ClassesInterface::class);
$locator
->method('getClasses')
->willReturn([
new \ReflectionClass(Employee::class),
new \ReflectionClass(Executive::class),
new \ReflectionClass(Person::class),
]);

$schema = (new Compiler())->compile($r, [
new Embeddings($locator, $reader),
new Entities($locator, $reader),
new TableInheritance($reader),
new ResetTables(),
new MergeColumns($reader),
new GenerateRelations(),
new RenderTables(),
new RenderRelations(),
new MergeIndexes($reader),
new SyncTables(),
new GenerateTypecast(),
]);

$this->assertSame('executives', $schema['executive'][SchemaInterface::TABLE]);
$this->assertNull($schema['employee'][SchemaInterface::TABLE] ?? null);
$this->assertSame('people', $schema['person'][SchemaInterface::TABLE]);
}
}

0 comments on commit abd3c24

Please sign in to comment.