mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Calling setAssociationOverride in prePersist/preUpdate throws an exception #7516
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 @jkabat on GitHub (May 30, 2025).
Bug Report
After upgrade to v3 I'm facing following exception "Typed property Doctrine\ORM\Mapping\ClassMetadata::$namingStrategy must not be accessed before initialization"
It might be caused by the fact naming strategy service is removed in compiler pass?
@jkabat commented on GitHub (Jun 2, 2025):
During debugging I've found out removal of following config resolves the issue
I will do more tests, but in the past I had to keep the setting for one of my bundles.
@ostrolucky could it be a problem with DoctrineBundle?
Also removing metadata_cache_driver can lead to performance degradation?
@ostrolucky commented on GitHub (Jun 2, 2025):
I don't think accessing uninitialized property is a problem with a bundle
@jkabat commented on GitHub (Jun 2, 2025):
@ostrolucky thank you for quick response
@jkabat commented on GitHub (Jun 2, 2025):
metadata_cache_driver removal was false positive. Sometimes it works, sometimes it is not and since it only happens in PROD environment I cant easily find the logic behind it.
My containers are having cache cleared before first usage. When I'm debugging, after running cache:warmup and reloading of phpfpm (clearing opcode cache), I saw few times that ClassMetadata had namingStrategy properly instantiated and everything worked. By repeating the request it failed again.
EDIT:
Any clues @greg0ire ?
@jkabat commented on GitHub (Jun 3, 2025):
I realized doctrine metadata are using 2 cache drivers in my production environment (depends of the cache driver setup): APC and PHPArray. When class metadata are reloaded from APC cache, "namingStrategy" property was not warmed up/instantiated, hence the exception.
It happens on line https://github.com/doctrine/orm/blob/3.3.x/src/Mapping/ClassMetadata.php#L1396
However disabling of APC cache did not solve the problem. I wonder how come nobody has not found the issue yet...
@greg0ire commented on GitHub (Jun 14, 2025):
I'm reproducing the bug by adding
$addressMetadata = unserialize(serialize($addressMetadata));here:05c8c5f114/tests/Tests/ORM/Mapping/ClassMetadataTest.php (L776)The "bug" is slightly different if I try it on 2.x (it complains about the object being
null, because there is no property type).I'm using quotes because nobody ever complained about this on 2.x, where the code is quite different and maybe
mapFieldandsetAttributeOverrideon 2.x are never called on a class metadata pulled from the cache, and maybe I failed to preserve this while refactoring this for 3.x 🤔It would be great if you could get a stack trace for your error, so that we can understand which method calls
_validateAndCompleteAssociationMapping@Bryce-Colton commented on GitHub (Jun 16, 2025):
We are experiencing a similar issue.
We have some code that manipulate metadata to add some relation at runtime (to handle custom fields).
Here is the sample :
If container running in
We got "Typed property Doctrine\ORM\Mapping\ClassMetadata::$namingStrategy must not be accessed before initialization"
If container running in
It works like expected.
NB : Clear cache command is executed each time container are recreated with env changes. We have reproduced on remote staging server and on local machine (where cache files are mounted to the container).
We hope this helps you identify the root cause of these bugs.
Here the stack trace from profiler (see Request/Response => exception)
Html report : doctrine-orm-stack-trace.txt
@greg0ire commented on GitHub (Jun 16, 2025):
@jkabat are you maybe manipulating metadata at runtime as well?
@Bryce-Colton why does it need to be at runtime in your case? Seems inefficient 🤔
@jkabat commented on GitHub (Jun 17, 2025):
@greg0ire yes, I'm overriding association before specific PERSIST/UPDATE. Unfortunately I couldn't find proper way to make it work without this "hack" (default association is OK for all hydrate operations).
Exception happens in this method:
...and then in
https://github.com/doctrine/orm/blob/3.3.x/src/Mapping/ClassMetadata.php#L1396as said before.Temporary solution for me is to bind namingStrategy property of ClassMetadata with the value from doctrine configuration just before calling setAssociationOverride.
@greg0ire commented on GitHub (Jun 17, 2025):
How / why was it working in 2.x ? Maybe it was because the naming strategy, which was, I think,
null, was never actually used in code triggered bysetAssociationOverride? I guess that code includes_validateAndCompleteAssociationMappingat some point?@jkabat commented on GitHub (Jun 18, 2025):
Yes,
setAssociationOverridetriggers_validateAndCompleteAssociationMapping.In v2 was not all properties strict typed and maybe in case of NULL, default namingStrategy was used. Hence no exception.@Bryce-Colton commented on GitHub (Jun 18, 2025):
I can confirm that the bug only occurs when attempting to modify the metadata by calling methods that internally trigger _validateAndCompleteAssociationMapping() (e.g. setAssociationOverride(), mapOneToOne(), etc.).
Like @jkabat, I implemented a workaround to ensure that the namingStrategy property is initialized before calling these methods.
Here is the patch I applied: gist
And an example of how I use it in my code:
@greg0ire : Thank you in advance for the work you're doing on this — it’s not an easy one, but your contribution is really appreciated.
@jkabat commented on GitHub (Jun 18, 2025):
Thanks for sharing your workaround @Bryce-Colton
I used closure bind technique (🙈) to initialize private property (until I get rid of setAssociationOverride or the issue gets a proper fix).