DDC-3843: indexBy collection loses index key after calling a ->matching(criteria) on it #4709

Open
opened 2026-01-22 14:48:04 +01:00 by admin · 13 comments
Owner

Originally created by @doctrinebot on GitHub (Jul 22, 2015).

Originally assigned to: @beberlei on GitHub.

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);
    }
}
Originally created by @doctrinebot on GitHub (Jul 22, 2015). Originally assigned to: @beberlei on GitHub. 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); } } ```
admin added the Bug label 2026-01-22 14:48:04 +01:00
Author
Owner

@vlastv commented on GitHub (Dec 8, 2015):

+1

@vlastv commented on GitHub (Dec 8, 2015): +1
Author
Owner

@Ocramius commented on GitHub (Dec 9, 2015):

Related: #5539

@Ocramius commented on GitHub (Dec 9, 2015): Related: #5539
Author
Owner

@ureimers commented on GitHub (Mar 15, 2016):

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.

@ureimers commented on GitHub (Mar 15, 2016): 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.
Author
Owner

@chmielot commented on GitHub (Apr 15, 2016):

+1

@chmielot commented on GitHub (Apr 15, 2016): +1
Author
Owner

@maresja1 commented on GitHub (May 20, 2016):

+1

@maresja1 commented on GitHub (May 20, 2016): +1
Author
Owner

@ericnilo commented on GitHub (Jun 1, 2016):

+1

@ericnilo commented on GitHub (Jun 1, 2016): +1
Author
Owner

@internalsystemerror commented on GitHub (Feb 22, 2017):

+1

@internalsystemerror commented on GitHub (Feb 22, 2017): +1
Author
Owner

@janwebdev commented on GitHub (Apr 3, 2017):

+1

@janwebdev commented on GitHub (Apr 3, 2017): +1
Author
Owner

@pavliktomas commented on GitHub (Jan 18, 2018):

+1

@pavliktomas commented on GitHub (Jan 18, 2018): +1
Author
Owner

@pesu commented on GitHub (Jun 23, 2018):

+1

@pesu commented on GitHub (Jun 23, 2018): +1
Author
Owner

@beberlei commented on GitHub (Feb 16, 2020):

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));
@beberlei commented on GitHub (Feb 16, 2020): 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: ```php new ArrayCollection($persister->loadCriteria($criteria)); ```
Author
Owner

@rela589n commented on GitHub (May 29, 2024):

Hi @greg0ire , if you have some free time, could you look at this?

@rela589n commented on GitHub (May 29, 2024): Hi @greg0ire , if you have some free time, could you look at this?
Author
Owner

@greg0ire commented on GitHub (May 29, 2024):

I have other priorities, sorry.

@greg0ire commented on GitHub (May 29, 2024): I have other priorities, sorry.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#4709