Composite key issue when joining #7187

Open
opened 2026-01-22 15:46:19 +01:00 by admin · 0 comments
Owner

Originally created by @tomatov-net on GitHub (Jul 17, 2023).

Bug Report

Q A
BC Break yes
Version 2.13.4

Summary

When we join a sub entity, with a composite primary key, we get the same entity for all the primary ones.

Current behavior

I have a feeling that when doctrine builds an object from the query result, it doesn’t care about uniqueness of the sub objects and just use the same one all the parents.

How to reproduce

We have the next structure: Cart→offer→fees

One Cart belongs to one offer, each offer may have several fees. offer_id and type are a composite primary key of the Fee.

Cart:
	id
	offer_id
	price

Offer:
	id
	name

Fee:
	offer_id
	type
	amount

Cart.xml.orm

<many-to-one field="offer" target-entity="Offer">
  <join-column name="offer_id" referenced-column-name="id"/>
</many-to-one>

Offer.xml.orm:

<one-to-many field="fees" target-entity="Fee" mapped-by="offer">
</one-to-many>

Fee.xml.orm:

<many-to-one field="offer" target-entity="Offer" inversed-by="fees">
  <join-column name="offer_id" referenced-column-name="id" nullable="false" />
</many-to-one>

<id name="offer_id" association-key="true" />
<id name="type" column="type" type="integer"/>

When we join Fee via Offer, for some reason we have the same Fee for all the Cart objects. The SQL is fine when we debug it, it’s valid, but something is wrong with mapping after running the query.

$result = $this->createQueryBuilder('c')
            ->select('c')
            ->join('c.offer', 'o')
            ->leftJoin('o.fees', 'if')
            ->getQuery()
            ->getResult();

Now we have an array of 2 Cart objects after the query finished. However, the nested object Fee is exactly the same: $result[0]→offer→fees === $result[1]→offer→fees , but in the database they are different.

Expected behavior

Each nested entity should be unique and joined correctly, as it was described in the ORM file.

This problem could be solved by adding its own unique id field into the Fee model, with it everything will work fine, as we've tested it many times. But for just a composite key without own id, doctrine fails to get the correct result 🎲

Originally created by @tomatov-net on GitHub (Jul 17, 2023). ### Bug Report <!-- Fill in the relevant information below to help triage your issue. --> | Q | A |------------ | ------ | BC Break | yes | Version | 2.13.4 #### Summary When we join a sub entity, with a composite primary key, we get the same entity for all the primary ones. #### Current behavior I have a feeling that when doctrine builds an object from the query result, it doesn’t care about uniqueness of the sub objects and just use the same one all the parents. #### How to reproduce We have the next structure: Cart→offer→fees One Cart belongs to one offer, each offer may have several fees. `offer_id` and `type` are a **composite primary key** of the Fee. ```jsx Cart: id offer_id price Offer: id name Fee: offer_id type amount ``` Cart.xml.orm ```xml <many-to-one field="offer" target-entity="Offer"> <join-column name="offer_id" referenced-column-name="id"/> </many-to-one> ``` Offer.xml.orm: ```xml <one-to-many field="fees" target-entity="Fee" mapped-by="offer"> </one-to-many> ``` Fee.xml.orm: ```xml <many-to-one field="offer" target-entity="Offer" inversed-by="fees"> <join-column name="offer_id" referenced-column-name="id" nullable="false" /> </many-to-one> <id name="offer_id" association-key="true" /> <id name="type" column="type" type="integer"/> ``` When we join Fee via Offer, for some reason we have the same Fee for all the Cart objects. The SQL is fine when we debug it, it’s valid, but something is wrong with mapping after running the query. ```php $result = $this->createQueryBuilder('c') ->select('c') ->join('c.offer', 'o') ->leftJoin('o.fees', 'if') ->getQuery() ->getResult(); ``` Now we have an array of **2** Cart objects after the query finished. However, the nested object `Fee` is exactly the same: `$result[0]→offer→fees === $result[1]→offer→fees` , but in the database they are different. #### Expected behavior Each nested entity should be unique and joined correctly, as it was described in the ORM file. This problem could be solved by adding its own unique `id` field into the `Fee` model, with it everything will work fine, as we've tested it many times. But for just a composite key without own `id`, doctrine fails to get the correct result 🎲
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7187