Simple Derived Identity issue #6153

Closed
opened 2026-01-22 15:27:49 +01:00 by admin · 6 comments
Owner

Originally created by @fabienlem on GitHub (Jan 10, 2019).

Originally assigned to: @Ocramius on GitHub.

Hello,

I'm struggling with an issue based on the use case that is described here: Use-Case 2: Simple Derived Identity

Q A
Version 2.5.14

Support Question

I have the following Doctrine entities and mapping in my Symfony app:

class User
{
    private $entity_id;

    private $address;

    ...
}

class Address
{
    private $user;

    ...
}

AppBundle\Entity\User:
    type: entity
    id:
        entity_id:
            type: integer
            generator:
                strategy: AUTO
    oneToOne:
        address:
            targetEntity: Address
            mappedBy: user
            cascade: ["persist"]

AppBundle\Entity\Address:
    type: entity
    id:
        user:
            associationKey: true
    oneToOne:
        user:
            targetEntity: User
            inversedBy: address
            joinColumn:
                name: entity_id
                referencedColumnName: entity_id

Every time I perform a DQL query that involves the User entity, Doctrine performs one additional query per matching User to retrieve the corresponding Address entity. That happen every time, even if the Address data are never used in the code.

I tried to reproduce this issue on a vanilla Symfony installation, and I faced another issue, I'm not able to perform the following code as I get an error (Entity of type AppBundle\Entity\Address is missing an assigned ID for field 'user'):

$user = (new User())->setAddress(new Address());
$entityManager->persist($user);
$entityManager->flush();

Do you have any hint of what is wrong?

Best regards

Originally created by @fabienlem on GitHub (Jan 10, 2019). Originally assigned to: @Ocramius on GitHub. Hello, I'm struggling with an issue based on the use case that is described here: [Use-Case 2: Simple Derived Identity](https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/tutorials/composite-primary-keys.html#use-case-2-simple-derived-identity) | Q | A |------------ | ----- | Version | 2.5.14 ### Support Question I have the following Doctrine entities and mapping in my Symfony app: ``` class User { private $entity_id; private $address; ... } class Address { private $user; ... } AppBundle\Entity\User: type: entity id: entity_id: type: integer generator: strategy: AUTO oneToOne: address: targetEntity: Address mappedBy: user cascade: ["persist"] AppBundle\Entity\Address: type: entity id: user: associationKey: true oneToOne: user: targetEntity: User inversedBy: address joinColumn: name: entity_id referencedColumnName: entity_id ``` Every time I perform a DQL query that involves the User entity, Doctrine performs one additional query **per matching User** to retrieve the corresponding Address entity. That happen every time, even if the Address data are never used in the code. I tried to reproduce this issue on a vanilla Symfony installation, and I faced another issue, I'm not able to perform the following code as I get an error (```Entity of type AppBundle\Entity\Address is missing an assigned ID for field 'user'```): ``` $user = (new User())->setAddress(new Address()); $entityManager->persist($user); $entityManager->flush(); ``` Do you have any hint of what is wrong? Best regards
admin added the Question label 2026-01-22 15:27:49 +01:00
admin closed this issue 2026-01-22 15:27:49 +01:00
Author
Owner

@Ocramius commented on GitHub (Jan 10, 2019):

This is correct behavior in a one-to-one association, because your Address is the owning side of the association, so the User needs to load it.

@Ocramius commented on GitHub (Jan 10, 2019): This is correct behavior in a one-to-one association, because your `Address` is the owning side of the association, so the `User` needs to load it.
Author
Owner

@fabienlem commented on GitHub (Jan 10, 2019):

Thank you @Ocramius for your reply.

But the problem is that if I retrieve 10,000 Users, 10,000 queries are performed to retrieve Addresses... Is it possible to change this behavior?

The following code doesn't help:

$queryBuilder->addSelect('a')->join('u.address', 'a')
@fabienlem commented on GitHub (Jan 10, 2019): Thank you @Ocramius for your reply. But the problem is that if I retrieve 10,000 Users, 10,000 queries are performed to retrieve Addresses... Is it possible to change this behavior? The following code doesn't help: ``` $queryBuilder->addSelect('a')->join('u.address', 'a') ```
Author
Owner

@Ocramius commented on GitHub (Jan 10, 2019):

A fetch-join should definitely remove this behavior: can you try an upgrade to 2.6.x and see if there's a change, first?

@Ocramius commented on GitHub (Jan 10, 2019): A fetch-join should definitely remove this behavior: can you try an upgrade to 2.6.x and see if there's a change, first?
Author
Owner

@fabienlem commented on GitHub (Jan 10, 2019):

I don't take advantage of PHP 7.1 for now... so I can't upgrade :(

I confirm the issue happen even with a fetch-join.

@fabienlem commented on GitHub (Jan 10, 2019): I don't take advantage of PHP 7.1 for now... so I can't upgrade :( I confirm the issue happen even with a fetch-join.
Author
Owner

@Ocramius commented on GitHub (Jan 10, 2019):

Yeah, on this end we'll need you to check on 2.6+ first, and otherwise provide a failing test case against this test suite.

We don't support 2.5.x anymore, unless it is about security issues.

@Ocramius commented on GitHub (Jan 10, 2019): Yeah, on this end we'll need you to check on 2.6+ first, and otherwise provide a failing test case against this test suite. We don't support 2.5.x anymore, unless it is about security issues.
Author
Owner

@fabienlem commented on GitHub (Jan 10, 2019):

Ok! I continue to investigate on my side and I'll let you know if I solved the issue. Thank you a lot for your quick replies. Have a nice day!

@fabienlem commented on GitHub (Jan 10, 2019): Ok! I continue to investigate on my side and I'll let you know if I solved the issue. Thank you a lot for your quick replies. Have a nice day!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6153