mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
SecondLevelCache throws exception when there's a not cached relation in the entity #7323
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 @KDederichs on GitHub (Feb 13, 2024).
Bug Report
Summary
When you have a cached entity that references a non cached entity, you'll get an exception.
Current behavior
Trying to call any
findmethod on the entity repository of the cached entity will result in:How to reproduce
Now call for example
findAll()onFooRepositoryExpected behavior
No exception
@Housik commented on GitHub (Jan 23, 2025):
I have same issue, tried to debug code and result is -> all associated classes must implement
#[ORM\Cache]too, otherwise exception is thrown. Not sure, if this is written in documentation.In
DefaultQueryCache.phpis following code:Exception is thrown, because in following code method
getCachedRegion()is not defined on $assocPersister. The reason is the association class does not implement#[ORM\Cache]attribute. In this case, $assocPersister is an instance ofBasicEntityPersisterclass not havinggetCachedRegion()method:line 303:
If I understand it correctly,
storeAssociationCache()method should not be executed at all - or better, store association to cache should not be done, if association entity is not marked as#[ORM\Cache]But as I said, temporary solution is to add
#[ORM\Cache]to all associations of cached class (to the class definition, close to#[ORM\Entity])@svolikmartin commented on GitHub (Nov 11, 2025):
So I got myself into this same problem, but here are some more peculiarities
This problem only propagates when:
If you load via find(id) or queryBuilder and where/setParameter(), it's okay, even if I do leftJoin on some property which has Cache Attribute, it won't cache it until you get child entity straightforward, so it works fine and does not matter if you do/don't have Cache in children. The fact that queryBuilder does not use SLC is another problem resp. missing feature.
But if I load via findOneBy() - in my case using serverId (uuid), it automatically tries to load and cache all children OneToOne properties with mappedBy (meaning the parent ID is in other table with FK and unique index), even if they are marked as EXTRA_LAZY, and if child does not have Cache it will throw this error.
I also found som irregularities when using find(id) method:
I know that this feature is marked as experimental and has some bugs, but I expected at least some consistency in what and when is cached 😆
Edit: I found out why id 11 was not cached - it was already in UnitOfWork loaded via Account->suppliers because I was authenticated as account which is master to this particular ID. If I used other account, suddenly ID 11 is, in fact, in Redis.
This is due to that Doctrine performs the following internally (simplified):