-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Add getSingleScalarResultOrNull method #11296
base: 3.4.x
Are you sure you want to change the base?
Conversation
Handly method for retrieves scalars which can not exists.
There hasn't been any activity on this pull request in the past 90 days, so it has been marked as stale and it will be closed automatically if no further activity occurs in the next 7 days. |
@derrabus will be merged? |
Sorry that nobody has picked up this PR yet. I'm honestly not nery eager to add more utility methods to our query objects and I can't picture myself using the one proposed here. Can you elaborate a bit more why you feel we need this method in the ORM? |
@derrabus in my case it helps to write more clean client code, my point is to keep try/catch db exceptions at ORM side, so calling function can satisfy single responsibility. |
{ | ||
try { | ||
return $this->getSingleResult(self::HYDRATE_SINGLE_SCALAR); | ||
} catch (NoResultException) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't use exceptions for flow control, they're expensive.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@greg0ire what do you mean? getOneOrNullResult
for examle already use NoResultException
. Or you point is to completely remove NoResultException
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh right we already do this. But let's not add more code with the same bad practice. I mean, why is no result considered exceptional in the first place?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But I also think you can completely remove it. Look at the code of getSingleResult
.
if ($this->_hydrationMode !== self::HYDRATE_SINGLE_SCALAR && ! $result) {
throw new NoResultException();
}
So maybe it's just the comment on line 797 that is wrong? Can you find a way to call getSingleScalarResult()
and get a NoResultException
?
The line was added in 4781dc0
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After reading the thread at #7871 I am now confused as to what the right way would be. That $hydrationMode
vs $this->hydrationMode
thing that I didn't spot is hard to grasp.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad, this behavior is actually proven by tests:
orm/tests/Tests/ORM/Functional/QueryTest.php
Lines 465 to 470 in e7efded
public function testGetSingleScalarResultThrowsExceptionOnNoResult(): void | |
{ | |
$this->expectException(NoResultException::class); | |
$this->_em->createQuery('select a.id from Doctrine\Tests\Models\CMS\CmsArticle a') | |
->getSingleScalarResult(); | |
} |
@greg0ire I am not a maintainer of any open source framework, but as far as I understand, their main goal is to save development time by covering the most common use cases. So here are my thoughts:
No offense :) |
!? Wouldn't replacing the body of your method with $result = $this->execute(null, $hydrationMode);
if (! is_array($result)) {
return $result;
}
if (count($result) > 1) {
throw new NonUniqueResultException();
}
return array_shift($result); be one way to do exactly that?
None taken? Why would I be offended by anything you said? |
Then
Just in case :) |
Unfortunately, based on the public function getOneOrNullResult(string|int|null $hydrationMode = null): mixed
{
try {
$result = $this->execute(null, $hydrationMode);
} catch (NoResultException) {
return null;
} |
Handly method for retrieves scalars which may not exist.