orm icon indicating copy to clipboard operation
orm copied to clipboard

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

Open doctrinebot opened this issue 10 years ago • 13 comments

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);
    }
}

doctrinebot avatar Jul 22 '15 00:07 doctrinebot

+1

vlastv avatar Dec 08 '15 08:12 vlastv

Related: #5539

Ocramius avatar Dec 09 '15 14:12 Ocramius

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 avatar Mar 15 '16 16:03 ureimers

+1

chmielot avatar Apr 15 '16 18:04 chmielot

+1

maresja1 avatar May 20 '16 15:05 maresja1

+1

ericnilo avatar Jun 01 '16 09:06 ericnilo

+1

internalsystemerror avatar Feb 22 '17 06:02 internalsystemerror

+1

janwebdev avatar Apr 03 '17 12:04 janwebdev

+1

pavliktomas avatar Jan 18 '18 12:01 pavliktomas

+1

pesu avatar Jun 23 '18 09:06 pesu

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 avatar Feb 16 '20 19:02 beberlei

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

rela589n avatar May 29 '24 11:05 rela589n

I have other priorities, sorry.

greg0ire avatar May 29 '24 13:05 greg0ire