DDC-3170: SimpleObjectHydrator fails to get discriminator column from mapped SQL result #3928

Open
opened 2026-01-22 14:31:38 +01:00 by admin · 2 comments
Owner

Originally created by @doctrinebot on GitHub (Jun 17, 2014).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user ureimers:

When querying a simple entity which uses single table- or class table inheritance using simple object hydration (AbstractQuery::HYDRATE_SIMPLEOBJECT), the mapped discriminator column is not retrieved correctly.

Example:

/****
 * @Entity
 * @InheritanceType("JOINED")
 * @DiscriminatorColumn(name="type", type="string")
 * @DiscriminatorMap({"product" = "Product"})
 */
abstract class AbstractEntity
{
    /*** @Id @Column(type="integer") @GeneratedValue **/
    public $id;
}

/****
 * @Entity
 */
class Product extends AbstractEntity
{
}

$manager->createQueryBuilder()
                ->select('p')
                ->from('Product, 'p')
                ->getQuery()
                ->getResult(AbstractQuery::HYDRATE_SIMPLEOBJECT);

// -> Exception: [Doctrine\ORM\Internal\Hydration\HydrationException] The discriminator column "type" is missing for "Product" using the DQL alias "p".

The SQL statement used is equal to:

SELECT r0*.id AS id0, r0_.type AS type1 FROM Product r1_ INNER JOIN AbstractEntity r0_ ON r1_.id = r0*.id

As you can see, the type column is given an alias of type1. This is saved in the ResultSetMapping of the request but not taken into account when actually retrieving the discriminator column back from the SQL result.

The problem is inside SimpleObjectHydrator#hydrateRowData().

When the discriminator column name is fetched via

$discrColumnName = $this->_platform->getSQLResultCasing($this->class->discriminatorColumn['name']);

the result is simply type which is wrong because the alias is type2. This can be fixed by adding

if ($metaMappingDiscrColumnName = array*search($discrColumnName, $this->*rsm->metaMappings)) {
    $discrColumnName = $metaMappingDiscrColumnName;
}

right after the column retrieval, because then the alias of the meta field type is correctly taken into account.

I'll create a PR with a unit test for this fix right after this ticket's creation.

I hope I'm doing everything right, this is my first contribution.

Originally created by @doctrinebot on GitHub (Jun 17, 2014). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user ureimers: When querying a simple entity which uses single table- or class table inheritance using simple object hydration (`AbstractQuery::HYDRATE_SIMPLEOBJECT`), the mapped discriminator column is not retrieved correctly. Example: ``` /**** * @Entity * @InheritanceType("JOINED") * @DiscriminatorColumn(name="type", type="string") * @DiscriminatorMap({"product" = "Product"}) */ abstract class AbstractEntity { /*** @Id @Column(type="integer") @GeneratedValue **/ public $id; } /**** * @Entity */ class Product extends AbstractEntity { } $manager->createQueryBuilder() ->select('p') ->from('Product, 'p') ->getQuery() ->getResult(AbstractQuery::HYDRATE_SIMPLEOBJECT); // -> Exception: [Doctrine\ORM\Internal\Hydration\HydrationException] The discriminator column "type" is missing for "Product" using the DQL alias "p". ``` The SQL statement used is equal to: ``` sql SELECT r0*.id AS id0, r0_.type AS type1 FROM Product r1_ INNER JOIN AbstractEntity r0_ ON r1_.id = r0*.id ``` As you can see, the `type` column is given an alias of `type1`. This is saved in the `ResultSetMapping` of the request but not taken into account when actually retrieving the discriminator column back from the SQL result. The problem is inside [SimpleObjectHydrator#hydrateRowData()](https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Internal/Hydration/SimpleObjectHydrator.php#L69). When the discriminator column name is fetched via ``` $discrColumnName = $this->_platform->getSQLResultCasing($this->class->discriminatorColumn['name']); ``` the result is simply `type` which is wrong because the alias is `type2`. This can be fixed by adding ``` if ($metaMappingDiscrColumnName = array*search($discrColumnName, $this->*rsm->metaMappings)) { $discrColumnName = $metaMappingDiscrColumnName; } ``` right after the column retrieval, because then the alias of the meta field `type` is correctly taken into account. I'll create a PR with a unit test for this fix right after this ticket's creation. I hope I'm doing everything right, this is my first contribution.
admin added the Bug label 2026-01-22 14:31:38 +01:00
Author
Owner

@doctrinebot commented on GitHub (Jun 17, 2014):

Comment created by ureimers:

PR is https://github.com/doctrine/doctrine2/pull/1060

I though that I first create a ticket, then propose a fix for that with a PR and by adding the Ticket ID in the PR-Title they are linked automatically. But unfortunately it seems I've done something wrong there.

@doctrinebot commented on GitHub (Jun 17, 2014): Comment created by ureimers: PR is https://github.com/doctrine/doctrine2/pull/1060 I though that I first create a ticket, then propose a fix for that with a PR and by adding the Ticket ID in the PR-Title they are linked automatically. But unfortunately it seems I've done something wrong there.
Author
Owner

@doctrinebot commented on GitHub (Jun 17, 2014):

Comment created by @doctrinebot:

A related Github Pull-Request [GH-1060] was closed:
https://github.com/doctrine/doctrine2/pull/1060

@doctrinebot commented on GitHub (Jun 17, 2014): Comment created by @doctrinebot: A related Github Pull-Request [GH-1060] was closed: https://github.com/doctrine/doctrine2/pull/1060
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#3928