Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct hydration of resultsets coming from DQL queries where the root of the selection is aliased and also the root of an inheritance #6367

Merged
merged 1 commit into from
May 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,8 @@ protected function hydrateRowData(array $row, array &$result)

// Get a reference to the parent object to which the joined element belongs.
if ($this->_rsm->isMixed && isset($this->rootAliases[$parentAlias])) {
$first = reset($this->resultPointers);
$parentObject = $first[key($first)];
$objectClass = $this->resultPointers[$parentAlias];
$parentObject = $objectClass[key($objectClass)];
} else if (isset($this->resultPointers[$parentAlias])) {
$parentObject = $this->resultPointers[$parentAlias];
} else {
Expand Down
144 changes: 144 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/GH6362Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\Tests\OrmFunctionalTestCase;
use Doctrine\ORM\Query;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\Tests\Mocks\HydratorMockStatement;

final class GH6362Test extends OrmFunctionalTestCase
{
protected function setUp()
{
parent::setUp();

$this->_schemaTool->createSchema(
[
$this->_em->getClassMetadata(GH6362Start::class),
$this->_em->getClassMetadata(GH6362Base::class),
$this->_em->getClassMetadata(GH6362Child::class),
$this->_em->getClassMetadata(GH6362Join::class),
]
);
}

/**
* @group 6362
*
* SELECT a as base, b, c, d
* FROM Start a
* LEFT JOIN a.bases b
* LEFT JOIN Child c WITH b.id = c.id
* LEFT JOIN c.joins d
*/
public function testInheritanceJoinAlias()
{
$rsm = new ResultSetMapping;
$rsm->addEntityResult(GH6362Start::class, 'a', 'base');
$rsm->addJoinedEntityResult(GH6362Base::class, 'b', 'a', 'bases');
$rsm->addEntityResult(GH6362Child::class, 'c');
$rsm->addJoinedEntityResult(GH6362Join::class, 'd', 'c', 'joins');

$rsm->addFieldResult('a', 'id_0', 'id');
$rsm->addFieldResult('b', 'id_1', 'id');
$rsm->addFieldResult('c', 'id_2', 'id');
$rsm->addFieldResult('d', 'id_3', 'id');

$rsm->addMetaResult('a', 'bases_id_4', 'bases_id', false, 'integer');
$rsm->addMetaResult('b', 'type_5', 'type');
$rsm->addMetaResult('c', 'type_6', 'type');
$rsm->addMetaResult('d', 'child_id_7', 'child_id', false, 'integer');

$rsm->setDiscriminatorColumn('b', 'type_5');
$rsm->setDiscriminatorColumn('c', 'type_6');

$resultSet = [
[
'id_0' => '1',
'id_1' => '1',
'id_2' => '1',
'id_3' => '1',
'bases_id_4' => '1',
'type_5' => 'child',
'type_6' => 'child',
'child_id_7' => '1',
],
];

$stmt = new HydratorMockStatement($resultSet);
$hydrator = new \Doctrine\ORM\Internal\Hydration\ObjectHydrator($this->_em);
$result = $hydrator->hydrateAll($stmt, $rsm, [Query::HINT_FORCE_PARTIAL_LOAD => true]);

$this->assertInstanceOf(GH6362Start::class, $result[0]['base']);
$this->assertInstanceOf(GH6362Child::class, $result[1][0]);
}
}

/**
* @Entity
*/
class GH6362Start
{
/**
* @Column(type="integer")
* @Id
* @GeneratedValue
*/
protected $id;

/**
* @ManyToOne(targetEntity="GH6362Base", inversedBy="starts")
*/
private $bases;
}

/**
* @InheritanceType("SINGLE_TABLE")
* @DiscriminatorColumn(name="type", type="string")
* @DiscriminatorMap({"child" = "GH6362Child"})
* @Entity
*/
abstract class GH6362Base
{
/**
* @Column(type="integer")
* @Id
* @GeneratedValue
*/
protected $id;

/**
* @OneToMany(targetEntity="GH6362Start", mappedBy="bases")
*/
private $starts;
}

/**
* @Entity
*/
class GH6362Child extends GH6362Base
{
/**
* @OneToMany(targetEntity="GH6362Join", mappedBy="child")
*/
private $joins;
}

/**
* @Entity
*/
class GH6362Join
{
/**
* @Column(type="integer")
* @Id
* @GeneratedValue
*/
private $id;

/**
* @ManyToOne(targetEntity="GH6362Child", inversedBy="joins")
*/
private $child;
}