DDC-3843: indexBy collection loses index key after calling a ->matching(criteria) on it
Jira issue originally created by user bitonda:
When I retrieve a filtered collection of entities which has indexBy attribute it loses index keys in the result. Before calling matching() method i tried to call toArray() and It solved the problem, I don't know if it is a bug .
class Entity {
/****
* @var ArrayCollection
* @ORM\OneToMany(targetEntity="Entity\Arcansel\BasketItem",mappedBy="userSession",indexBy="id",cascade={"persist","remove","merge"})
*/
protected $basketItems;
// Some comments here
public function getBasketItems()
{
$date = DateTime::newDateTimeUniversal();
$criteria = Criteria::create()->where(Criteria::expr()->gt("addTime",$date));
// next line of code prevents returned collection to lose indexBy keys
$a = $this->basketItems->toArray();
return $this->basketItems->matching($criteria);
}
}
+1
Related: #5539
Also having the same issue.
Calling toArray() solves the problem because the collection becomes initialized (loaded entirely from the DB). If a collection is initialized, matching() doesn't trigger a DB-Query but acts directly on the already loaded collection.
But calling toArray() as a workaround before calling matching() effectively kills the benefit of using matching() in the first place.
+1
+1
+1
+1
+1
+1
+1
The problem here is that the line in PersistentCollection#matching must be changed to iterate over the results and build the indexed by map afterwards if indexBy isset:
new ArrayCollection($persister->loadCriteria($criteria));
Hi @greg0ire , if you have some free time, could you look at this?
I have other priorities, sorry.