Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Hidden fields triggering error when using getSingleScalarResult() #8340

Next Next commit
Fix Hidden fields triggering error when using getSingleScalarResult()
Fixes #4257
HIDDEN fields was causing the "unicity" check to fail (NonUniqueResultException), because we was counting raw data instead of gathered row data.
  • Loading branch information
Mediagone committed Nov 13, 2020
commit 8f384e5b40b59b25bc7aa4a0bf035fe1ba28b055
6 changes: 3 additions & 3 deletions lib/Doctrine/ORM/Internal/Hydration/SingleScalarHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ protected function hydrateAllData()
throw new NonUniqueResultException('The query returned multiple rows. Change the query or use a different result function like getScalarResult().');
}

if (count($data[key($data)]) > 1) {
$result = $this->gatherScalarRowData($data[key($data)]);

if (count($result) > 1) {
throw new NonUniqueResultException('The query returned a row containing multiple columns. Change the query or use a different result function like getScalarResult().');
}

$result = $this->gatherScalarRowData($data[key($data)]);

return array_shift($result);
}
}
75 changes: 75 additions & 0 deletions tests/Doctrine/Tests/ORM/Hydration/SingleScalarHydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,79 @@ public function testHydrateSingleScalar($name, $resultSet)
$hydrator->hydrateAll($stmt, $rsm);
}
}




public static function singleScalarResultSetWithHiddenFieldProvider(): array
{
return [
// valid
[
'name' => 'result1',
'resultSet' => [
[
'u__id' => '1',
'u__name' => 'romanb',
],
],
],
// valid
[
'name' => 'result2',
'resultSet' => [
[
'u__name' => 'romanb',
],
],
],
// invalid
[
'name' => 'result3',
'resultSet' => [
[
'u__id' => '1',
],
[
'u__id' => '2',
],
],
],
];
}

/**
*
* @dataProvider singleScalarResultSetWithHiddenFieldProvider
*/
public function testHydrateSingleScalarWithHiddenField($name, $resultSet)
Mediagone marked this conversation as resolved.
Show resolved Hide resolved
{
$rsm = new ResultSetMapping;
$rsm->addScalarResult('u__id', 'id', 'string');

$stmt = new HydratorMockStatement($resultSet);
$hydrator = new SingleScalarHydrator($this->_em);

if ($name === 'result1') {
$result = $hydrator->hydrateAll($stmt, $rsm);
$this->assertEquals('1', $result);

return;
}

if ($name === 'result2') {
$result = $hydrator->hydrateAll($stmt, $rsm);
$this->assertEquals(null, $result);

return;
}

if ($name === 'result3') {
$this->expectException(NonUniqueResultException::class);
$hydrator->hydrateAll($stmt, $rsm);

return;
}
}

}