Skip to content

Enum type not correctly detected in DQL query result #344

@janlanger

Description

@janlanger

Consider this setup:

#[ORM\Entity]
class ProductCountry extends Entity
{

	#[ORM\Id]
	#[ORM\GeneratedValue]
	#[ORM\Column]
	private ?int $id = null;

	#[ORM\Column]
	#[Set(visibility: 'private')]
	private Country $country;
}

enum Country: string
{
    case CZE = 'cze';
    //....
}

when doing DQL to country from this entity

public function getUsedCountriesInTravelProducts(): array
{
	$query = $this->entityManager->createQueryBuilder()
		->select('pc.country')
		->from(ProductCountry::class, 'pc')
		// .. some conditions
		->addGroupBy('pc.country')
		->getQuery();
	return array_map(static fn (array $result): Country => $result['country'], $query->getResult());
}

phpstan reports error Anonymous function should return Country but returns string but in reality, $result['country'] is properly hydrated to enum

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions