Skip to content

Commit

Permalink
Hydrator: Don't spill properly qualified relation cols onto base model
Browse files Browse the repository at this point in the history
fixes #123
  • Loading branch information
nilmerg committed Jan 11, 2024
1 parent 3a6f687 commit 205d3a9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
14 changes: 8 additions & 6 deletions src/Hydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ protected function updateColumnToTargetMap(string $path, array $columnToProperty
public function hydrate(array $data, Model $model)
{
$defaultsToApply = [];
$columnToTargetMap = $this->columnToTargetMap;
foreach ($this->hydrators as $path => $vars) {
list($target, $relation, $columnToPropertyMap, $defaults) = $vars;

Expand Down Expand Up @@ -143,7 +144,7 @@ public function hydrate(array $data, Model $model)
}
}

$subject->setProperties($this->extractAndMap($data, $columnToPropertyMap, $path));
$subject->setProperties($this->extractAndMap($data, $columnToPropertyMap, $path, $columnToTargetMap));
$this->query->getResolver()->getBehaviors($target)->retrieve($subject);
$defaultsToApply[] = [$subject, $defaults];
}
Expand Down Expand Up @@ -205,20 +206,21 @@ public function hydrate(array $data, Model $model)
* @param array $data
* @param array $columnToPropertyMap
* @param string $path
* @param array $columnToTargetMap
*
* @return array
*/
protected function extractAndMap(array &$data, array $columnToPropertyMap, string $path)
protected function extractAndMap(array &$data, array $columnToPropertyMap, string $path, array &$columnToTargetMap)

Check failure on line 213 in src/Hydrator.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.2 on ubuntu-latest

Method ipl\Orm\Hydrator::extractAndMap() has parameter $columnToTargetMap with no value type specified in iterable type array.

Check failure on line 213 in src/Hydrator.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.3 on ubuntu-latest

Method ipl\Orm\Hydrator::extractAndMap() has parameter $columnToTargetMap with no value type specified in iterable type array.

Check failure on line 213 in src/Hydrator.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.4 on ubuntu-latest

Method ipl\Orm\Hydrator::extractAndMap() has parameter $columnToTargetMap with no value type specified in iterable type array.

Check failure on line 213 in src/Hydrator.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.0 on ubuntu-latest

Method ipl\Orm\Hydrator::extractAndMap() has parameter $columnToTargetMap with no value type specified in iterable type array.

Check failure on line 213 in src/Hydrator.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.1 on ubuntu-latest

Method ipl\Orm\Hydrator::extractAndMap() has parameter $columnToTargetMap with no value type specified in iterable type array.

Check failure on line 213 in src/Hydrator.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.2 on ubuntu-latest

Method ipl\Orm\Hydrator::extractAndMap() has parameter $columnToTargetMap with no value type specified in iterable type array.
{
$extracted = [];
foreach (array_intersect_key($columnToPropertyMap, $data) as $column => $property) {
$extracted[$property] = $data[$column];

if (isset($this->columnToTargetMap[$column][$path])) {
unset($this->columnToTargetMap[$column][$path]);
if (empty($this->columnToTargetMap[$column])) {
if (isset($columnToTargetMap[$column][$path])) {
unset($columnToTargetMap[$column][$path]);
if (empty($columnToTargetMap[$column])) {
// Only unset a column once it's really not required anymore
unset($data[$column], $this->columnToTargetMap[$column]);
unset($data[$column], $columnToTargetMap[$column]);
}
}
}
Expand Down
28 changes: 26 additions & 2 deletions tests/HydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,33 @@ public function testWhetherAmbiguousColumnsAreCorrectlyMapped(): void
$hydrator->hydrate(['subsystem_audit_user_id' => 2], $subject);

$this->assertTrue(isset($subject->audit->user_id), 'Ambiguous columns are not mapped correctly');
$this->assertSame($subject->audit->user_id, 2, 'Ambiguous columns are not mapped correctly');
$this->assertSame(2, $subject->audit->user_id, 'Ambiguous columns are not mapped correctly');

$this->assertTrue(isset($subject->audit->user->id), 'Ambiguous columns are not mapped correctly');
$this->assertSame($subject->audit->user->id, 2, 'Ambiguous columns are not mapped correctly');
$this->assertSame(2, $subject->audit->user->id, 'Ambiguous columns are not mapped correctly');
}

public function testWhetherProperlyQualifiedColumnsAreOnlyPassedOnToMatchingTargets()
{
$query = Car::on(new TestConnection())
->with('user_custom_keys');

$hydrator = $query->createHydrator();

$subject = new Car();
$hydrator->hydrate(['car_user_custom_keys_username' => 'foo'], $subject);

$subject2 = new Car();
$hydrator->hydrate(['car_user_custom_keys_username' => 'bar'], $subject2);

$this->assertFalse(
isset($subject->user->custom_keys_username),
'This should not fail. If it does, there is a new issue'
);

$this->assertFalse(
isset($subject2->user->custom_keys_username),
'Properly qualified relation columns are spilled onto the base model'
);
}
}

0 comments on commit 205d3a9

Please sign in to comment.