Skip to content

Support type infering of getArrayResult when selecting specific fields #269

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

Closed
janedbal opened this issue Jan 27, 2022 · 3 comments
Closed

Comments

@janedbal
Copy link
Contributor

Is there a possibility to support array hydration type infering when field list is selected? I can see it actually works, but is not supported for getArrayResult nor ->getResult(AbstractQuery::HYDRATE_ARRAY) even though the behaviour in doctrine is equal.

I can send failing tests if needed.

cc @arnaud-lb

@arnaud-lb
Copy link
Contributor

Hi @janedbal

Unfortunately, supporting more than one hydration mode is challenging for the reasons detailed here. So for now phpstan-doctrine supports only the HYDRATE_OBJECT mode.

As you pointed out, selecting individual fields is supported in HYDRATE_OBJECT mode, so in some cases it may be possible to use getResult() instead of getArrayResult():

$query = $em->createQuery('
    SELECT e.foo, e.bar FROM Entity e
');

$query->getResult(); // array<array{foo: string, bar: string}>

@janedbal
Copy link
Contributor Author

I see, thanks for quick response.

Sidenote: Sad to hear that using (semantically) improper HYDRATE_OBJECT in those cases (it cannot hydrate objects when custom fields are selected) is the only working approach, but I think the benefits of type-inference is huge enough to convert our codebase to use that and write some phpstan rule to ensure that it in future.

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants