Can I filter out results from an arrayCollection in Doctrine 2 while using lazy loading? For example,
// users = ArrayCollection with User entities containing an "active" property
$customer->users->filter('active' => TRUE)->first()
It's unclear for me how the filter method is actually used.
Your use case would be :
if you add ->first() you'll get only the first entry returned, which is not what you want.
@ Sjwdavies You need to put () around the variable you pass to USE. You can also shorten as in_array return's a boolean already:
The Boris Guéry answer's at this post, may help you: Doctrine 2, query inside entities
The
Collection#filter
method really does eager load all members. Filtering at the SQL level will be added in doctrine 2.3.Doctrine now has
Criteria
which offers a single API for filtering collections with SQL and in PHP, depending on the context.https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html#filtering-collections
Update
This will achieve the result in the accepted answer, without getting everything from the database.