Criteria Matching Ignoring In Memory Changes #5231

Open
opened 2026-01-22 15:02:08 +01:00 by admin · 3 comments
Owner

Originally created by @awhitby on GitHub (Aug 25, 2016).

This seems to be the same issue reported in https://github.com/doctrine/doctrine2/issues/3046, however

It appears that if I make a change to an entities field without flushing, the subsequent matching call with a criteria queries the database for the association and returns incorrect results. The in memory changes are still applied though

Given this function:

    public function getActiveUsers()
    {
        $criteria = Criteria::create();
        $criteria->where(Criteria::expr()->eq('active', true));

        return $this->users->matching($criteria);
    }

Running the following will output "true true"

foreach($organisation->getActiveUsers() as $user)
    echo ($user->isActive() ? 'true' : 'false');

Marking one of those users as inactive and running the same criteria match will output "false true"

User::get(1)->setActive(false);

foreach($organisation->getActiveUsers() as $user)
    echo ($user->isActive() ? 'true' : 'false');

This bug is forcing me to perform a flush before every criteria query which is extremely draining on performance. If anyone can shed some light on this it would be appreciated

Originally created by @awhitby on GitHub (Aug 25, 2016). This seems to be the same issue reported in https://github.com/doctrine/doctrine2/issues/3046, however It appears that if I make a change to an entities field without flushing, the subsequent matching call with a criteria queries the database for the association and returns incorrect results. The in memory changes are still applied though Given this function: ``` public function getActiveUsers() { $criteria = Criteria::create(); $criteria->where(Criteria::expr()->eq('active', true)); return $this->users->matching($criteria); } ``` Running the following will output "true true" ``` foreach($organisation->getActiveUsers() as $user) echo ($user->isActive() ? 'true' : 'false'); ``` Marking one of those users as inactive and running the same criteria match will output "false true" ``` User::get(1)->setActive(false); foreach($organisation->getActiveUsers() as $user) echo ($user->isActive() ? 'true' : 'false'); ``` This bug is forcing me to perform a flush before every criteria query which is extremely draining on performance. If anyone can shed some light on this it would be appreciated
admin added the BugMissing Tests labels 2026-01-22 15:02:08 +01:00
Author
Owner

@akomm commented on GitHub (Jun 19, 2017):

Same issue in onFlush. Only here I can not flush, so I have to do something like $entity->collection->toArray() to force a fetch & merge. A better workaround is appreciated.

@akomm commented on GitHub (Jun 19, 2017): Same issue in onFlush. Only here I can not flush, so I have to do something like $entity->collection->toArray() to force a fetch & merge. A better workaround is appreciated.
Author
Owner

@lcobucci commented on GitHub (Jun 23, 2017):

@awhitby could you please send us a PR with a functional test that reproduces this behaviour? Sending just an example doesn't give us the complete overview of things and why it fails. You can find examples on 971c400025/tests/Doctrine/Tests/ORM/Functional

@lcobucci commented on GitHub (Jun 23, 2017): @awhitby could you please send us a PR with a functional test that reproduces this behaviour? Sending just an example doesn't give us the complete overview of things and why it fails. You can find examples on https://github.com/doctrine/doctrine2/tree/971c40002522cfebe58d80aff21eef9fe439fa60/tests/Doctrine/Tests/ORM/Functional
Author
Owner

@dmontero commented on GitHub (Nov 17, 2017):

Same problem here. I'm trying to run a criteria::matching after persist but it doesn't see the new elements being persisted.
If I flush, then it works

@dmontero commented on GitHub (Nov 17, 2017): Same problem here. I'm trying to run a criteria::matching after persist but it doesn't see the new elements being persisted. If I flush, then it works
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5231