mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
PersistentCollection:matching() does not initialize the collection #5432
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 @solor2001 on GitHub (Feb 27, 2017).
I'm using OneToMany relation with fetch="LAZY".
Using matching(Criteria) with where() expression works on collection PK only. Other fields of collection are null, because the flag isDirty == false.
To get correct behavior I have to call initialize() first before using matching().
@lcobucci commented on GitHub (Feb 27, 2017):
@solor2001 could you please reproduce this as a failing functional test (you can use
e4704beaf9/tests/Doctrine/Tests/ORM/Functional/Ticket/GH5562Test.phpas example).@ihorsamusenko commented on GitHub (Jun 29, 2017):
@lcobucci although this is not a failing functional test but at least it explains the nature of the issue.
Consider the following case:
We have two rows in database, say orders with status
unpaid.We also have a user which owns those orders. User entity has the following method:
In some part of the app we retrieve one of those orders from
entityManagerand set the status topaid. Now, we have two orders with statusunpaidin db and we have onepaidand oneunpaidin memory.If the
$user->ordersis instance of AbstractLazyCollection and has not been initialized yet, then calling$user->getOrders('unpaid')will return two orders (including the one which haspaidstatus in memory butunpaidin db)I can think of two workarounds:
$this->orders->initialize()or$this->orders->toArray()so the method becomes like the following:Which can be something not affordable (in case the collection is too big)
$em->flush()before calling$user->getOrders(). Which is something I'd like to avoid.Are there others?
@lcobucci commented on GitHub (Jul 23, 2017):
@samusenkoiv to be honest I wouldn't have a bidirectional association on your example, a repository (or even a query) seems much more appropriated IMO.
We do need a failing test case to understand it better and fix (if we agree it's something to be fixed).
@anokhinAleksey commented on GitHub (Jul 18, 2018):
I have the same issue whith Persistent Collection. In my case there are two entities User and UserRole with ManyToMany association:
I need to find out if user has role matching some array of roles:
If collection of roles is not initialized at the moment when I call matching(), I`ll get an Doctrine\DBAL\Exception\SyntaxErrorException while executing sql query. So I have to initialize collection explicitly, like this: