Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(autorefresh): return fresh data from RepositoryDecorator methods#983

Merged
nikophil merged 1 commit into
zenstruck:2.xfrom
nikophil:fix/repository-object-unfresh
Sep 24, 2025
Merged

fix(autorefresh): return fresh data from RepositoryDecorator methods#983
nikophil merged 1 commit into
zenstruck:2.xfrom
nikophil:fix/repository-object-unfresh

Conversation

@nikophil
Copy link
Copy Markdown
Member

@nikophil nikophil commented Sep 21, 2025

fixes #975

The problem did exist before autorefresh with lazy objects, but we just never noticed it, and it was hidden by proxy mechanism 🤷

The root problem here is that EntityRepository::findBy() (and all other related methods like findAll(), etc..) do not actually return a fresh object if the object is already managed.

What's surprising is that Doctrine actually performs the query to the db, but it just don't hydrate the object, unless a hint is passed. This optimization is just here to prevent hydration. The behavior is the same in ORM and in ODM

This is annoying, because hints can only be passed if a query builder is used. So we cannot use anymore ObjectRepository::findBy() and other nice helper methods

* @var \WeakMap<object, bool>
*/
private static $buffer = [];
private static \WeakMap $buffer;
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

finally I'm using a weak map 😄

this prevents to have duplicates in the buffer

@nikophil nikophil force-pushed the fix/repository-object-unfresh branch from b4301f4 to 0acd710 Compare September 24, 2025 15:38
@nikophil nikophil merged commit 90866d2 into zenstruck:2.x Sep 24, 2025
73 checks passed
@nikophil nikophil deleted the fix/repository-object-unfresh branch September 24, 2025 15:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Factory::all() loads outdated entities

1 participant