Association doesn't work for an identifier with a custom name #5660

Closed
opened 2026-01-22 15:14:00 +01:00 by admin · 2 comments
Owner

Originally created by @automatix on GitHub (Aug 30, 2017).

Hello!

In my application there are entities Foo, FooA extends Foo, and FooB extends Foo. They build a Single Table Inheritance structure.

The FooA#testX refers (ManyToOne) the entity TextX:

/**
 * @var TestX
 *
 * @ORM\ManyToOne(targetEntity="TestX")
 * @ORM\JoinColumn(name="resource_id", referencedColumnName="id")
 */
private $testX;

The FooB#testB refers (ManyToOne) the entity TextY:

/**
 * @var TestY
 *
 * @ORM\ManyToOne(targetEntity="TestY")
 * @ORM\JoinColumn(name="resource_id", referencedColumnName="name")
 */
private $testY;

As you can see, the only difference is, that the entity TestX' identifier is id and the TestY' identifier is name.

Now I want to retrieve all Foo entities and display the data:

$repository = $this->entityManager->getRepository(Foo::class);
$entities = $repository->findAll();
foreach ($entities as $entity) {
    switch (get_class($entity)) {
        case FooTestX::class:
            /** @var TestX $loggedObject */
            $loggedObject = $entity->getTestX();
            echo 'FooTestX' . ' : ' . $loggedObject->getId();
            break;
        case FooTestY::class:
            /** @var TestY $loggedObject */
            $loggedObject = $entity->getTestY();
            echo 'FooTestY' . ' : ' . $loggedObject->getName();
            break;
    };
}

For the FooTestX entities (and the TestX entities retrieved from them) the results are as exprected -- I get the data. But for TestY entities no data seems to be loaded. All their properties are empty and the identifier property name gets the value 0. So when in the next step I try to get its data (e.g. with $loggedObject->getDescription();), it leads to an error, since Doctrine then tries to load a not existent entity with name 0.

For me it looks like a bug. Or is it by design, that only entities with the identifier id can be referenced in associations? Just looked at another case in my code with a ManyToOne reference to the TestY entity. There it's not in the context of the inheritance. And it works...

Thanks

Originally created by @automatix on GitHub (Aug 30, 2017). Hello! In my application there are entities `Foo`, `FooA extends Foo`, and `FooB extends Foo`. They build a [Single Table Inheritance](http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html#single-table-inheritance) structure. The `FooA#testX` refers (`ManyToOne`) the entity `TextX`: /** * @var TestX * * @ORM\ManyToOne(targetEntity="TestX") * @ORM\JoinColumn(name="resource_id", referencedColumnName="id") */ private $testX; The `FooB#testB` refers (`ManyToOne`) the entity `TextY`: /** * @var TestY * * @ORM\ManyToOne(targetEntity="TestY") * @ORM\JoinColumn(name="resource_id", referencedColumnName="name") */ private $testY; As you can see, the only difference is, that the entity `TestX`' identifier is `id` and the `TestY`' identifier is `name`. Now I want to retrieve all `Foo` entities and display the data: $repository = $this->entityManager->getRepository(Foo::class); $entities = $repository->findAll(); foreach ($entities as $entity) { switch (get_class($entity)) { case FooTestX::class: /** @var TestX $loggedObject */ $loggedObject = $entity->getTestX(); echo 'FooTestX' . ' : ' . $loggedObject->getId(); break; case FooTestY::class: /** @var TestY $loggedObject */ $loggedObject = $entity->getTestY(); echo 'FooTestY' . ' : ' . $loggedObject->getName(); break; }; } For the `FooTestX` entities (and the `TestX` entities retrieved from them) the results are as exprected -- I get the data. But for `TestY` entities no data seems to be loaded. All their properties are empty and the identifier property `name` gets the value `0`. So when in the next step I try to get its data (e.g. with `$loggedObject->getDescription();`), it leads to an error, since Doctrine then tries to load a not existent entity with `name` `0`. For me it looks like a bug. <strike>Or is it by design, that only entities with the identifier `id` can be referenced in associations?</strike> Just looked at another case in my code with a `ManyToOne` reference to the `TestY` entity. There it's not in the context of the inheritance. And it works... Thanks
admin added the BugMissing Tests labels 2026-01-22 15:14:00 +01:00
admin closed this issue 2026-01-22 15:14:00 +01:00
Author
Owner

@Majkl578 commented on GitHub (Aug 30, 2017):

Can you please create a failing test case for this issue? PR with failing test would be helpful.
You can find some examples among the existing tests: https://github.com/doctrine/doctrine2/tree/master/tests/Doctrine/Tests/ORM/Functional/Ticket -- you can just copy one and adjust it to your needs.

@Majkl578 commented on GitHub (Aug 30, 2017): Can you please create a failing test case for this issue? PR with failing test would be helpful. You can find some examples among the existing tests: https://github.com/doctrine/doctrine2/tree/master/tests/Doctrine/Tests/ORM/Functional/Ticket -- you can just copy one and adjust it to your needs.
Author
Owner

@beberlei commented on GitHub (Dec 8, 2020):

Closed for missing tests, scenario too complex to recreate ourselves.

@beberlei commented on GitHub (Dec 8, 2020): Closed for missing tests, scenario too complex to recreate ourselves.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5660