From 6ac325d60df653c9332ebc43d22f4de5bfe38ce1 Mon Sep 17 00:00:00 2001 From: Wolfy-J Date: Sat, 27 Apr 2019 13:35:37 +0300 Subject: [PATCH] improved support for single table inheritance --- src/Compiler.php | 15 ++++++++++++++- src/Generator/RenderTables.php | 5 +++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Compiler.php b/src/Compiler.php index 6ceef1b..59f34c3 100644 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -10,6 +10,7 @@ use Cycle\ORM\Schema; use Cycle\Schema\Definition\Entity; +use Doctrine\Common\Inflector\Inflector; use Spiral\Database\Exception\CompilerException; final class Compiler @@ -86,7 +87,7 @@ protected function compute(Registry $registry, Entity $entity) // table inheritance foreach ($registry->getChildren($entity) as $child) { $this->result[$child->getClass()] = [Schema::ROLE => $entity->getRole()]; - $schema[Schema::CHILDREN][] = $child->getClass(); + $schema[Schema::CHILDREN][$this->childAlias($child)] = $child->getClass(); } ksort($schema); @@ -171,4 +172,16 @@ protected function getPrimary(Entity $entity): string throw new CompilerException("Entity `{$entity->getRole()}` must have defined primary key"); } + + /** + * Return the unique alias for the child entity. + * + * @param Entity $entity + * @return string + */ + protected function childAlias(Entity $entity): string + { + $r = new \ReflectionClass($entity->getClass()); + return Inflector::classify($r->getShortName()); + } } \ No newline at end of file diff --git a/src/Generator/RenderTables.php b/src/Generator/RenderTables.php index ba60df7..bc679f8 100644 --- a/src/Generator/RenderTables.php +++ b/src/Generator/RenderTables.php @@ -8,6 +8,7 @@ namespace Cycle\Schema\Generator; +use Cycle\ORM\Mapper\Mapper; use Cycle\Schema\Definition\Entity; use Cycle\Schema\GeneratorInterface; use Cycle\Schema\Registry; @@ -70,6 +71,10 @@ protected function compute(Registry $registry, Entity $entity) $column->render($table->column($field->getColumn())); } + if ($registry->getChildren($entity) !== []) { + $table->string(Mapper::ENTITY_TYPE, 32); + } + if (count($primaryKeys)) { $table->setPrimaryKeys($primaryKeys); }