PersistentCollection::get with fetch EXTRA_LAZY fetches not associated entities #6198

Open
opened 2026-01-22 15:28:43 +01:00 by admin · 1 comment
Owner

Originally created by @crtl on GitHub (Mar 6, 2019).

Bug Report

Q A
Version 2.6.3

Summary

Calling Doctrine\ORM\PersistentCollection::get returns an entity even if it is not in the collection.
The relation has to be configured as fetch="EXTRA_LAZY" and has to have an index-by config.

Current behavior

Calling get returns an entity even if it is not part of the collection

How to reproduce

  1. Create a self referencing many-to-many relationship
  2. Configure it to: fetch EXTRA_LAZY and index-by id
  3. Call get on an empty collection with an entity id that exists but is not related
  4. The method will return the entity

Expected behavior

get should not return an entity when its not associated, even when the entity exists.

Example:

<doctrine-mapping>
  <entity class="MyEntity">
     <id name="id" type="integer">
       <generator strategy="AUTO" />
     </id>

     <!-- ... -->

     <many-to-many field="entityRelationCollection" target-entity="MyEntity" fetch="EXTRA_LAZY" index-by="id">

         <!-- ... -->

      </many-to-many>
  </entity>
</doctrine-mapping>
//Imagine we have 2 entities with ids: 1, 2

$entity1 = $entityRepository->find(1);
$collection = $entity1->getEntityRelationCollection();

$collection->get(2) //Will return entity with id=2 even if it was not added and persisted before
$collection->containsKey(2) //Will return false

Originally created by @crtl on GitHub (Mar 6, 2019). ### Bug Report <!-- Fill in the relevant information below to help triage your issue. --> | Q | A |------------ | ------ | Version | 2.6.3 #### Summary Calling `Doctrine\ORM\PersistentCollection::get` returns an entity even if it is not in the collection. The relation has to be configured as `fetch="EXTRA_LAZY"` and has to have an `index-by` config. #### Current behavior Calling `get` returns an entity even if it is not part of the collection #### How to reproduce 1. Create a self referencing `many-to-many` relationship 2. Configure it to: fetch `EXTRA_LAZY` and index-by `id` 3. Call `get` on an empty collection with an entity id that exists but is not related 4. The method will return the entity #### Expected behavior `get` should not return an entity when its not associated, even when the entity exists. ### Example: ```xml <doctrine-mapping> <entity class="MyEntity"> <id name="id" type="integer"> <generator strategy="AUTO" /> </id> <!-- ... --> <many-to-many field="entityRelationCollection" target-entity="MyEntity" fetch="EXTRA_LAZY" index-by="id"> <!-- ... --> </many-to-many> </entity> </doctrine-mapping> ``` ```php //Imagine we have 2 entities with ids: 1, 2 $entity1 = $entityRepository->find(1); $collection = $entity1->getEntityRelationCollection(); $collection->get(2) //Will return entity with id=2 even if it was not added and persisted before $collection->containsKey(2) //Will return false ```
Author
Owner

@Ocramius commented on GitHub (Mar 8, 2019):

If I get this right, this looks like a missing criteria filtering by collection owner in the persister logic.

@Ocramius commented on GitHub (Mar 8, 2019): If I get this right, this looks like a missing criteria filtering by collection owner in the persister logic.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6198