Skip to content

Commit

Permalink
Add ability to use Property names and Column names in relation keys (#47
Browse files Browse the repository at this point in the history
)

Co-authored-by: thenotsoft <[email protected]>
Co-authored-by: roxblnfk <[email protected]>
  • Loading branch information
3 people authored Dec 27, 2021
1 parent dc10cda commit 203534f
Show file tree
Hide file tree
Showing 32 changed files with 293 additions and 204 deletions.
4 changes: 2 additions & 2 deletions src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private function compute(Registry $registry, Entity $entity): void
sprintf(
'Unable to apply schema modifier `%s` for the `%s` role. %s',
$modifier::class,
(string)$entity->getRole(),
$entity->getRole(),
$e->getMessage()
),
(int)$e->getCode(),
Expand Down Expand Up @@ -181,7 +181,7 @@ private function renderColumns(Entity $entity): array
$comparator->compare();
} catch (Throwable $e) {
throw new Exception\CompilerException(
sprintf("Error compiling the `%s` role.\n\n%s", (string)$entity->getRole(), $e->getMessage()),
sprintf("Error compiling the `%s` role.\n\n%s", $entity->getRole(), $e->getMessage()),
$e->getCode()
);
}
Expand Down
16 changes: 9 additions & 7 deletions src/Generator/GenerateRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ final class GenerateRelations implements GeneratorInterface
private $relations = [];

/**
* @param array|null $relations
* @param array|null $relations
* @param OptionSchema|null $optionSchema
*/
public function __construct(array $relations = null, OptionSchema $optionSchema = null)
Expand All @@ -67,10 +67,12 @@ public function __construct(array $relations = null, OptionSchema $optionSchema

foreach ($relations as $id => $relation) {
if (!$relation instanceof RelationInterface) {
throw new \InvalidArgumentException(sprintf(
'Invalid relation type, RelationInterface excepted, `%s` given',
is_object($relation) ? get_class($relation) : gettype($relation)
));
throw new \InvalidArgumentException(
sprintf(
'Invalid relation type, RelationInterface excepted, `%s` given',
is_object($relation) ? get_class($relation) : gettype($relation)
)
);
}

$this->relations[$id] = $relation;
Expand All @@ -97,7 +99,7 @@ public function run(Registry $registry): Registry

/**
* @param Registry $registry
* @param Entity $entity
* @param Entity $entity
*/
protected function register(Registry $registry, Entity $entity): void
{
Expand Down Expand Up @@ -126,7 +128,7 @@ protected function register(Registry $registry, Entity $entity): void

/**
* @param Registry $registry
* @param Entity $entity
* @param Entity $entity
*/
protected function inverse(Registry $registry, Entity $entity): void
{
Expand Down
4 changes: 2 additions & 2 deletions src/Generator/RenderTables.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace Cycle\Schema\Generator;

use Cycle\Database\Schema\AbstractTable;
use Cycle\Database\Schema\Reflector;
use Cycle\Schema\Definition\Entity;
use Cycle\Schema\GeneratorInterface;
use Cycle\Schema\Registry;
use Cycle\Schema\Table\Column;
use Cycle\Database\Schema\AbstractTable;
use Cycle\Database\Schema\Reflector;

/**
* Generate table columns based on entity definition.
Expand Down
15 changes: 9 additions & 6 deletions src/Relation/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,13 @@ public function compute(Registry $registry): void
$source = $registry->getEntity($this->source);
$target = $registry->getEntity($this->target);

$this->fixContextFields($source, $target);

// create target outer field
$this->createRelatedFields(
$target,
Relation::OUTER_KEY,
$registry->getTableSchema($target),
$source,
Relation::INNER_KEY
);
Expand All @@ -71,13 +74,13 @@ public function render(Registry $registry): void
$source = $registry->getEntity($this->source);
$target = $registry->getEntity($this->target);

$innerFields = $this->getFields($source, Relation::INNER_KEY);
$outerFields = $this->getFields($target, Relation::OUTER_KEY);
$sourceTable = $registry->getTableSchema($source);

$table = $registry->getTableSchema($source);
$innerFields = $this->getFields($source, Relation::INNER_KEY, $sourceTable);
$outerFields = $this->getFields($target, Relation::OUTER_KEY, $registry->getTableSchema($target));

if ($this->options->get(self::INDEX_CREATE) && $innerFields->count() > 0) {
$table->index($innerFields->getColumnNames());
$sourceTable->index($innerFields->getColumnNames());
}

if ($this->options->get(self::FK_CREATE)) {
Expand All @@ -99,8 +102,8 @@ public function inverseTargets(Registry $registry): array

/**
* @param RelationInterface $relation
* @param string $into
* @param int|null $load
* @param string $into
* @param int|null $load
*
* @throws RelationException
*
Expand Down
15 changes: 9 additions & 6 deletions src/Relation/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,13 @@ public function compute(Registry $registry): void
$source = $registry->getEntity($this->source);
$target = $registry->getEntity($this->target);

$this->fixContextFields($source, $target);

// create target outer field
$this->createRelatedFields(
$source,
Relation::INNER_KEY,
$registry->getTableSchema($source),
$target,
Relation::OUTER_KEY
);
Expand All @@ -80,13 +83,13 @@ public function render(Registry $registry): void
$source = $registry->getEntity($this->source);
$target = $registry->getEntity($this->target);

$innerFields = $this->getFields($source, Relation::INNER_KEY);
$outerFields = $this->getFields($target, Relation::OUTER_KEY);
$targetTable = $registry->getTableSchema($target);

$table = $registry->getTableSchema($target);
$innerFields = $this->getFields($source, Relation::INNER_KEY, $registry->getTableSchema($source));
$outerFields = $this->getFields($target, Relation::OUTER_KEY, $targetTable);

if ($this->options->get(self::INDEX_CREATE) && $outerFields->count() > 0) {
$table->index($outerFields->getColumnNames());
$targetTable->index($outerFields->getColumnNames());
}

if ($this->options->get(self::FK_CREATE)) {
Expand All @@ -108,8 +111,8 @@ public function inverseTargets(Registry $registry): array

/**
* @param RelationInterface $relation
* @param string $into
* @param int|null $load
* @param string $into
* @param int|null $load
*
* @throws RelationException
*
Expand Down
17 changes: 10 additions & 7 deletions src/Relation/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,15 @@ public function compute(Registry $registry): void
$source = $registry->getEntity($this->source);
$target = $registry->getEntity($this->target);

$this->fixContextFields($source, $target);

// create target outer field
$this->createRelatedFields(
$source,
Relation::INNER_KEY,
$registry->getTableSchema($source),
$target,
Relation::OUTER_KEY
Relation::OUTER_KEY,
);
}

Expand All @@ -71,13 +74,13 @@ public function render(Registry $registry): void
$source = $registry->getEntity($this->source);
$target = $registry->getEntity($this->target);

$innerFields = $this->getFields($source, Relation::INNER_KEY);
$outerFields = $this->getFields($target, Relation::OUTER_KEY);
$targetTable = $registry->getTableSchema($target);

$table = $registry->getTableSchema($target);
$innerFields = $this->getFields($source, Relation::INNER_KEY, $registry->getTableSchema($source));
$outerFields = $this->getFields($target, Relation::OUTER_KEY, $targetTable);

if ($this->options->get(self::INDEX_CREATE) && count($outerFields) > 0) {
$table->index($outerFields->getColumnNames());
$targetTable->index($outerFields->getColumnNames());
}

if ($this->options->get(self::FK_CREATE)) {
Expand All @@ -99,8 +102,8 @@ public function inverseTargets(Registry $registry): array

/**
* @param RelationInterface $relation
* @param string $into
* @param int|null $load
* @param string $into
* @param int|null $load
*
* @throws RelationException
*
Expand Down
5 changes: 5 additions & 0 deletions src/Relation/ManyToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,21 @@ public function compute(Registry $registry): void
));
}

$this->fixContextFields($source, $target);
$this->fixContextFields($source, $through, ['innerKey', 'outerKey', 'throughInnerKey', 'throughOuterKey']);

$this->createRelatedFields(
$source,
Relation::INNER_KEY,
$registry->getTableSchema($source),
$through,
Relation::THROUGH_INNER_KEY
);

$this->createRelatedFields(
$target,
Relation::OUTER_KEY,
$registry->getTableSchema($target),
$through,
Relation::THROUGH_OUTER_KEY
);
Expand Down
2 changes: 1 addition & 1 deletion src/Relation/Morphed/BelongsToMorphed.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function compute(Registry $registry): void

// create target outer field
foreach ($outerKeys as $key => $morphKey) {
$outerField = $outerFields->getByColumnName($key);
$outerField = $outerFields->get($key);

$this->ensureField(
$source,
Expand Down
3 changes: 2 additions & 1 deletion src/Relation/Morphed/MorphedHasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ public function compute(Registry $registry): void
$this->createRelatedFields(
$source,
Relation::INNER_KEY,
$registry->getTableSchema($source),
$target,
Relation::OUTER_KEY
Relation::OUTER_KEY,
);

// create target outer field
Expand Down
3 changes: 2 additions & 1 deletion src/Relation/Morphed/MorphedHasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ public function compute(Registry $registry): void
$this->createRelatedFields(
$source,
Relation::INNER_KEY,
$registry->getTableSchema($source),
$target,
Relation::OUTER_KEY
Relation::OUTER_KEY,
);

// create target outer field
Expand Down
13 changes: 9 additions & 4 deletions src/Relation/OptionSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@
*/
final class OptionSchema
{
private array $aliases = [];

private array $options = [];

private array $template = [];

private array $context = [];

public function __construct(array $aliases)
public function __construct(private array $aliases)
{
$this->aliases = $aliases;
}

public function __debugInfo(): array
Expand All @@ -38,6 +35,14 @@ public function __debugInfo(): array
return $result;
}

/**
* Get available options
*/
public function getOptions(): array
{
return $this->options;
}

/**
* Create new option set with user provided options.
*/
Expand Down
3 changes: 3 additions & 0 deletions src/Relation/RefersTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@ public function compute(Registry $registry): void
$source = $registry->getEntity($this->source);
$target = $registry->getEntity($this->target);

$this->fixContextFields($source, $target);

// create target outer field
$this->createRelatedFields(
$target,
Relation::OUTER_KEY,
$registry->getTableSchema($target),
$source,
Relation::INNER_KEY
);
Expand Down
48 changes: 24 additions & 24 deletions src/Relation/RelationSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,29 +89,6 @@ public function compute(Registry $registry): void
}
}

private function packSchema(): array
{
$schema = [];

foreach (static::RELATION_SCHEMA as $option => $template) {
if (in_array($option, static::EXCLUDE, true)) {
continue;
}

$schema[$option] = $this->options->get($option);
}

// load option is not required in schema
unset($schema[Relation::LOAD]);

return [
Relation::TYPE => static::RELATION_TYPE,
Relation::TARGET => $this->target,
Relation::LOAD => $this->getLoadMethod(),
Relation::SCHEMA => $schema,
];
}

protected function getLoadMethod(): ?int
{
if (!$this->options->has(Relation::LOAD)) {
Expand All @@ -137,7 +114,7 @@ protected function getOptions(): OptionSchema
*/
protected function getPrimaryColumns(Entity $entity): array
{
$columns = $entity->getPrimaryFields()->getColumnNames();
$columns = $entity->getPrimaryFields()->getNames();

if ($columns === []) {
throw new RegistryException("Entity `{$entity->getRole()}` must have defined primary key");
Expand Down Expand Up @@ -180,4 +157,27 @@ protected function hasIndex(
}
return false;
}

private function packSchema(): array
{
$schema = [];

foreach (static::RELATION_SCHEMA as $option => $template) {
if (in_array($option, static::EXCLUDE, true)) {
continue;
}

$schema[$option] = $this->options->get($option);
}

// load option is not required in schema
unset($schema[Relation::LOAD]);

return [
Relation::TYPE => static::RELATION_TYPE,
Relation::TARGET => $this->target,
Relation::LOAD => $this->getLoadMethod(),
Relation::SCHEMA => $schema,
];
}
}
Loading

0 comments on commit 203534f

Please sign in to comment.