Skip to content

Commit

Permalink
improved support for single table inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfy-j committed Apr 27, 2019
1 parent c4e0406 commit 6ac325d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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());
}
}
5 changes: 5 additions & 0 deletions src/Generator/RenderTables.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 6ac325d

Please sign in to comment.