1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
<?php
declare(strict_types=1);
namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\ORM\AbstractQuery;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\DiscriminatorColumn;
use Doctrine\ORM\Mapping\DiscriminatorMap;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\InheritanceType;
use Doctrine\Tests\OrmFunctionalTestCase;
use PHPUnit\Framework\Attributes\Group;
#[Group('DDC-2306')]
class DDC3170Test extends OrmFunctionalTestCase
{
protected function setUp(): void
{
parent::setUp();
$this->createSchemaForModels(
DDC3170AbstractEntityJoined::class,
DDC3170ProductJoined::class,
DDC3170AbstractEntitySingleTable::class,
DDC3170ProductSingleTable::class,
);
}
/**
* Tests that the discriminator column is correctly read from the meta mappings when fetching a
* child from an inheritance mapped class.
*
* The simple object hydration maps the type field to a field alias like type2. This mapping needs
* to be considered when loading the discriminator column's value from the SQL result.
*
* {@see \Doctrine\ORM\Internal\Hydration\SimpleObjectHydrator::hydrateRowData()}
*/
public function testIssue(): void
{
$productJoined = new DDC3170ProductJoined();
$productSingleTable = new DDC3170ProductSingleTable();
$this->_em->persist($productJoined);
$this->_em->persist($productSingleTable);
$this->_em->flush();
$this->_em->clear();
$result = $this->_em->createQueryBuilder()
->select('p')
->from(DDC3170ProductJoined::class, 'p')
->getQuery()
->getResult(AbstractQuery::HYDRATE_SIMPLEOBJECT);
self::assertCount(1, $result);
self::assertContainsOnlyInstancesOf(DDC3170ProductJoined::class, $result);
$result = $this->_em->createQueryBuilder()
->select('p')
->from(DDC3170ProductSingleTable::class, 'p')
->getQuery()
->getResult(AbstractQuery::HYDRATE_SIMPLEOBJECT);
self::assertCount(1, $result);
self::assertContainsOnlyInstancesOf(DDC3170ProductSingleTable::class, $result);
}
}
#[Entity]
#[InheritanceType('JOINED')]
#[DiscriminatorColumn(name: 'type', type: 'string')]
#[DiscriminatorMap(['product' => 'DDC3170ProductJoined'])]
abstract class DDC3170AbstractEntityJoined
{
/** @var int */
#[Id]
#[Column(type: 'integer')]
#[GeneratedValue]
public $id;
}
#[Entity]
class DDC3170ProductJoined extends DDC3170AbstractEntityJoined
{
}
#[Entity]
#[InheritanceType('SINGLE_TABLE')]
#[DiscriminatorColumn(name: 'type', type: 'string')]
#[DiscriminatorMap(['product' => 'DDC3170ProductSingleTable'])]
abstract class DDC3170AbstractEntitySingleTable
{
/** @var int */
#[Id]
#[Column(type: 'integer')]
#[GeneratedValue]
public $id;
}
#[Entity]
class DDC3170ProductSingleTable extends DDC3170AbstractEntitySingleTable
{
}
|