Skip to content

Commit

Permalink
Hydrator: Properly hydrate custom columns if an alias prefix is used
Browse files Browse the repository at this point in the history
fixes #127
  • Loading branch information
nilmerg committed Jan 11, 2024
1 parent a68f501 commit 35b23fe
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Hydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ public function hydrate(array $data, Model $model)

// If there are any columns left, propagate them to the targeted relation if possible, to the base otherwise
foreach ($data as $column => $value) {
if (($aliasPrefix = $this->query->getResolver()->getAliasPrefix())) {
$column = substr($column, strlen($aliasPrefix));
}

$columnName = $column;
$steps = explode('_', $column);
$baseTable = array_shift($steps);
Expand Down
21 changes: 21 additions & 0 deletions tests/HydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,25 @@ public function testCustomAliasesForTheBaseTableAndRelationsWithUnderscoresInThe
'Custom aliases for relations are not correctly hydrated if their name contains an underscore'
);
}

public function testCustomColumnsAreProperlyHydratedIfAnAliasPrefixIsUsed()
{
$query = Car::on(new TestConnection());
$query->getResolver()->setAliasPrefix('test_');

$hydrator = $query->createHydrator();

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

$this->assertTrue(
isset($subject->wheel_size),
'Custom columns are not correctly hydrated if an alias prefix is in use'
);
$this->assertSame(
'xxl',
$subject->wheel_size,
'Custom columns are not correctly hydrated if an alias prefix is in use'
);
}
}

0 comments on commit 35b23fe

Please sign in to comment.