Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ parameters:
- tests/
ignoreErrors:
- message: '/Call to an undefined (static )?method Respect\\Relational\\(Sql|Db|Mapper)::\w+\(\)\./'
- message: '/Call to an undefined (static )?method Respect\\Data\\Collections\\(Collection|Composite|Typed)::\w+\(\)\./'
- message: '/Unsafe usage of new static\(\)\./'
- message: '/Cannot unset property .+ because it might have hooks in a subclass\./'
-
Expand Down
127 changes: 1 addition & 126 deletions src/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Respect\Data\AbstractMapper;
use Respect\Data\CollectionIterator;
use Respect\Data\Collections\Collection;
use Respect\Data\Collections\Composite;
use Respect\Data\Hydrator;
use Respect\Data\Hydrators\PrestyledAssoc;
use SplObjectStorage;
Expand Down Expand Up @@ -151,94 +150,6 @@ private function flushSingle(object $entity): void
}
}

/**
* Extract composed columns from parent, UPDATE child tables using FK relationship.
*
* For existing entities (UPDATE path), the parent PK is known and we can
* update the child table directly: UPDATE comment SET text=? WHERE post_id=?
*
* For new entities (INSERT path), child inserts happen after the parent
* via insertCompositionChildren().
*
* @param array<string, mixed> $cols
*
* @return array<string, mixed>
*/
private function extractAndOperateCompositions(Collection $collection, array $cols): array
{
if (!$collection instanceof Composite) {
return $cols;
}

$parentPk = $this->style->identifier($collection->name);
$parentPkValue = $cols[$parentPk] ?? null;
$fkToParent = $this->style->remoteIdentifier($collection->name);

foreach ($collection->compositions as $comp => $spec) {
$compCols = [];
foreach ($spec as $key) {
$dbKey = $this->style->realProperty($key);
if (!isset($cols[$dbKey])) {
continue;
}

$compCols[$dbKey] = $cols[$dbKey];
unset($cols[$dbKey]);
}

if ($parentPkValue === null || empty($compCols)) {
continue;
}

$this->db
->update($comp)
->set($compCols)
->where([[$fkToParent, '=', $parentPkValue]])
->exec();
}

return $cols;
}

private function insertCompositionChildren(Collection $collection, object|null $entity): void
{
if (!$collection instanceof Composite || $entity === null) {
return;
}

$parentPk = $this->style->identifier($collection->name);
$parentPkValue = $this->entityFactory->get($entity, $parentPk);

if ($parentPkValue === null) {
return;
}

$fkToParent = $this->style->remoteIdentifier($collection->name);
$entityCols = $this->entityFactory->extractColumns($entity);

foreach ($collection->compositions as $comp => $spec) {
$compCols = [];
foreach ($spec as $key) {
$dbKey = $this->style->realProperty($key);
if (!isset($entityCols[$key])) {
continue;
}

$compCols[$dbKey] = $entityCols[$key];
}

if (empty($compCols)) {
continue;
}

$compCols[$fkToParent] = $parentPkValue;
$this->db
->insertInto($comp, array_keys($compCols))
->values(array_values($compCols))
->exec();
}
}

/**
* @param array<string, mixed> $columns
*
Expand Down Expand Up @@ -271,7 +182,6 @@ private function rawDelete(
/** @param array<string, mixed> $columns */
private function rawUpdate(array $columns, Collection $collection): bool
{
$columns = $this->extractAndOperateCompositions($collection, $columns);
$condition = $this->guessCondition($columns, $collection);

return $this->db
Expand All @@ -287,8 +197,7 @@ private function rawInsert(
Collection $collection,
object|null $entity = null,
): bool {
$columns = $this->extractAndOperateCompositions($collection, $columns);
$result = $this->db
$result = $this->db
->insertInto($collection->name, array_keys($columns))
->values(array_values($columns))
->exec();
Expand All @@ -297,8 +206,6 @@ private function rawInsert(
$this->checkNewIdentity($entity, $collection);
}

$this->insertCompositionChildren($collection, $entity);

return $result;
}

Expand Down Expand Up @@ -352,18 +259,6 @@ private function buildSelectStatement(Sql $sql, array $collections): Sql
foreach ($this->entityFactory->enumerateFields($c->name) as $dbCol => $styledProp) {
$selectTable[] = self::aliasedColumn($tableSpecifier, $dbCol, $styledProp);
}

// Composition columns come after entity columns so they override on collision
if (!$c instanceof Composite) {
continue;
}

foreach ($c->compositions as $composition => $columns) {
$compPrefix = $tableSpecifier . Composite::COMPOSITION_MARKER . $composition;
foreach ($columns as $col) {
$selectTable[] = self::aliasedColumn($compPrefix, $col, $col);
}
}
}

return $sql->select(...$selectTable);
Expand Down Expand Up @@ -418,23 +313,6 @@ private function parseConditions(array &$conditions, Collection $collection, str
return $parsedConditions;
}

private function parseCompositions(Sql $sql, Collection $collection, string $entity): void
{
if (!$collection instanceof Composite) {
return;
}

foreach (array_keys($collection->compositions) as $comp) {
$alias = $entity . Composite::COMPOSITION_MARKER . $comp;
$sql->innerJoin($comp);
$sql->as($alias);
$sql->on([
$alias . '.' . $this->style->remoteIdentifier($entity)
=> $entity . '.' . $this->style->identifier($entity),
]);
}
}

/**
* @param array<string, string> $aliases
* @param array<mixed> $conditions
Expand Down Expand Up @@ -468,7 +346,6 @@ private function parseCollection(
//No parent collection means it's the first table in the query
if ($parentAlias === null) {
$sql->from($entity);
$this->parseCompositions($sql, $collection, $entity);

return;
}
Expand All @@ -479,8 +356,6 @@ private function parseCollection(
$sql->leftJoin($entity);
}

$this->parseCompositions($sql, $collection, $entity);

if ($alias !== $entity) {
$sql->as($alias);
}
Expand Down
Loading
Loading