mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
NULL returned instead of collection after update to 2.14.0 #7092
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @DigitalTimK on GitHub (Jan 5, 2023).
BC Break Report
Summary
After
composer update "doctrine/orm"I have an error during my phpunit (v9.5.27) tests. The returned value (children) isnullinstead of an collection.Previous behavior
tests are running w/o error
Current behavior
Tests raise an error:
TypeError: App\Entity\Pricing::getDiscounts(): Return value must be of type Doctrine\Common\Collections\Collection, null returnedHow to reproduce
This error seems to be happen only during PHPUNIT-test. I encounter no issue when calling this function in a controller or in twig.
@mbabker commented on GitHub (Jan 5, 2023):
As a suggested best practice, you should instantiate a default collection in your entity's constructor to ensure it has a valid state before it's persisted.
Not knowing how your test works, at the bare minimum, a
(new Pricing())->getDiscounts()would hit the same issue of returning null.@joshlopes commented on GitHub (Jan 5, 2023):
I'm actually getting the same, and on my constructor I'm setting an ArrayCollection, but the lazy proxy is returning null instead on my test and strict type failed.
The upgrade was suggested by dependabot but tests started failing.
The issue happens after the data is persisted, the proxy seems unable to fetch the right data or when it does the data is not what it is in the database, so something must be blocking it to properly initialise -- that would be my guess.
@DigitalTimK commented on GitHub (Jan 5, 2023):
Hey @mbabker.
thanks for this quick reaction. Actually I am initiating the variable in the constructor. I didn't put these details in my description above. I updated my initial bug report which is now showing the constructor.
@simonberger commented on GitHub (Jan 5, 2023):
There is a possibility your issues or at least one of those is fixed by #10362. It could be helpful if you try those changes on your tests.
@joshlopes commented on GitHub (Jan 5, 2023):
@simonberger it sounds like it does fix the current issue -- I just tried to manually load the data after using fixtures on my test and the test passed. So between loading fixtures and using it through proxies it fails, all proxy data is empty, only have id, rest of the fields are empty.
Thanks for all your work everyone 👌
@simonberger commented on GitHub (Jan 5, 2023):
Yes I expected the problem to be your private id field which the PR fixes. In case someone fights the problem a workaround can also be the change the visibility of the identifier to protected.
@DigitalTimK commented on GitHub (Jan 5, 2023):
I was also able to solve the issue. In my case I lost the data below the initial object (lazy loading?) by decomposing to much the object... so this is why the result was NULL.
Not sure if I shall close this ... at least my solution is a work around.... But at the end this new version created an issue that might need to be solved on doctrine's side.
@derrabus commented on GitHub (Jan 6, 2023):
If the problem is solved for you, feel free to close. But if we should accept it as a bug, we need a stable reproducer or a failing test. A reproducer would be a repository that I can check out and execute in order to reproduce your problem.
@DigitalTimK commented on GitHub (Jan 7, 2023):
So, I will close this item as I was not able to produce a failing specimen.
@flohw commented on GitHub (Jan 16, 2023):
Is it possible that this issue is related to #10336 ? I had the same behavior : using my fixtures just after they were initialized everything was fine but as soon as I started a functional test (EM::clear() is called), I got this error. Not on collection though but on standard property which were not initialized properly somewhere.
I downgraded to doctrine/orm:2.13.5 for now waiting for the next release.
@mpdude commented on GitHub (Jan 31, 2023):
@DigitalTimK did I get it right that you intended to close this issue?
@DigitalTimK commented on GitHub (Feb 1, 2023):
@mpdude yes, that correct.
@Herz3h commented on GitHub (Aug 11, 2023):
Just for information, this happened to me with ORM 2.9.5 after upgrading from php 7.4 to php 8.1 and after debugging, it seems that there was an "invalid mapping" exception that was silently thrown here: https://github.com/doctrine/orm/blob/2.16.x/lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php#L269
The reason is that one of my entity's relation, was implementing JsonSerializable, but didn't have a return type. Which threw this error:
Error: During inheritance of JsonSerializable: UncaughtI had to add a return type to
jsonSerialized()as required by php 8.1And that fixed my Collection being null problem