Skip to content

Commit

Permalink
Fix the support for enum types in the ResultSetMappingBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
stof committed Oct 18, 2023
1 parent 42af7ca commit 866283d
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/Doctrine/ORM/Query/ResultSetMappingBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ protected function addAllClassFields($class, $alias, $columnAliasMap = [])
}

$this->addFieldResult($alias, $columnAlias, $propertyName);

$enumType = $classMetadata->getFieldMapping($propertyName)['enumType'] ?? null;
if (! empty($enumType)) {
$this->addEnumResult($columnAlias, $enumType);
}
}

foreach ($classMetadata->associationMappings as $associationMapping) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\GH11017;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
*/
class GH11017Entity
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*
* @var ?int
*/
public $id;

/**
* @ORM\Column(type="string", enumType=GH11017Enum::class)
*
* @var GH11017Enum
*/
public $field;
}
11 changes: 11 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/GH11017/GH11017Enum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\GH11017;

enum GH11017Enum: string
{
case FIRST = 'first';
case SECOND = 'second';
}
49 changes: 49 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/GH11017/GH11017Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\GH11017;

use Doctrine\ORM\AbstractQuery;
use Doctrine\ORM\Query\ResultSetMappingBuilder;
use Doctrine\Tests\OrmFunctionalTestCase;

use function sprintf;

/**
* @requires PHP 8.1
*/
class GH11017Test extends OrmFunctionalTestCase
{
protected function setUp(): void
{
parent::setUp();

$this->setUpEntitySchema([
GH11017Entity::class,
]);
}

public function testPostPersistListenerUpdatingObjectFieldWhileOtherInsertPending(): void
{
$entity1 = new GH11017Entity();
$entity1->field = GH11017Enum::FIRST;
$this->_em->persist($entity1);

$this->_em->flush();
$this->_em->clear();

$rsm = new ResultSetMappingBuilder($this->_em, ResultSetMappingBuilder::COLUMN_RENAMING_INCREMENT);
$rsm->addRootEntityFromClassMetadata(GH11017Entity::class, 'e');

$tableName = $this->_em->getClassMetadata(GH11017Entity::class)->getTableName();
$sql = sprintf('SELECT %s FROM %s e WHERE id = :id', $rsm->generateSelectClause(), $tableName);

$query = $this->_em->createNativeQuery($sql, $rsm)
->setParameter('id', $entity1->id);

$entity1Reloaded = $query->getSingleResult(AbstractQuery::HYDRATE_ARRAY);
self::assertNotNull($entity1Reloaded);
self::assertSame($entity1->field, $entity1Reloaded['field']);
}
}

0 comments on commit 866283d

Please sign in to comment.