Query by discriminator type uses FQCN instead of discriminator value #6864

Open
opened 2026-01-22 15:40:21 +01:00 by admin · 1 comment
Owner

Originally created by @NotionCommotion on GitHub (Oct 31, 2021).

Bug Report

Q A
BC Break yes/no
Version 2.7.1-DEV

Summary

Query by discriminator type using INSTANCE OF uses FQCN instead of discriminator value.

Current behavior

return $this->createQueryBuilder('u')
->andWhere('u.email = :email')
->andWhere('u INSTANCE OF :discr')
->setParameters(['email'=>$email, 'discr'=>SuperUser::class])
->getQuery()
->getOneOrNullResult()
;
SELECT u0_.id AS id_0, etc, etc
FROM super_user s1_ INNER JOIN "user" u0_ ON s1_.id = u0_.id WHERE u0_.email = ? AND u0_.type IN (?)
["johndoe@gmail.com","App\\Entity\\User\\SuperUser"]

How to reproduce

#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\InheritanceType(value: 'JOINED')]
#[ORM\DiscriminatorColumn(name: 'type', type: 'string')]
#[ORM\DiscriminatorMap(value: ['tenant' => TenantUser::class, 'super' => SuperUser::class])]
#[ORM\Table(name: '`user`'])]
abstract class User
{
    // stuff...
}

Expected behavior

SELECT u0_.id AS id_0, etc, etc
FROM super_user s1_ INNER JOIN "user" u0_ ON s1_.id = u0_.id WHERE u0_.email = ? AND u0_.type IN (?)
["johndoe@gmail.com","super"]
Originally created by @NotionCommotion on GitHub (Oct 31, 2021). ### Bug Report <!-- Fill in the relevant information below to help triage your issue. --> | Q | A |------------ | ------ | BC Break | yes/no | Version | 2.7.1-DEV #### Summary Query by discriminator type using INSTANCE OF uses FQCN instead of discriminator value. #### Current behavior ``` return $this->createQueryBuilder('u') ->andWhere('u.email = :email') ->andWhere('u INSTANCE OF :discr') ->setParameters(['email'=>$email, 'discr'=>SuperUser::class]) ->getQuery() ->getOneOrNullResult() ; ``` ``` SELECT u0_.id AS id_0, etc, etc FROM super_user s1_ INNER JOIN "user" u0_ ON s1_.id = u0_.id WHERE u0_.email = ? AND u0_.type IN (?) ["johndoe@gmail.com","App\\Entity\\User\\SuperUser"] ``` #### How to reproduce ``` #[ORM\Entity(repositoryClass: UserRepository::class)] #[ORM\InheritanceType(value: 'JOINED')] #[ORM\DiscriminatorColumn(name: 'type', type: 'string')] #[ORM\DiscriminatorMap(value: ['tenant' => TenantUser::class, 'super' => SuperUser::class])] #[ORM\Table(name: '`user`'])] abstract class User { // stuff... } ``` #### Expected behavior ``` SELECT u0_.id AS id_0, etc, etc FROM super_user s1_ INNER JOIN "user" u0_ ON s1_.id = u0_.id WHERE u0_.email = ? AND u0_.type IN (?) ["johndoe@gmail.com","super"] ```
admin added the Bug label 2026-01-22 15:40:21 +01:00
Author
Owner

@kimhemsoe commented on GitHub (Jan 12, 2022):

Hello.

Never tried it, but i believe you need to pass param as a ClassMetadata, ex.

["discr" => $em->getClassMetadata(SuperUser::class)]

@kimhemsoe commented on GitHub (Jan 12, 2022): Hello. Never tried it, but i believe you need to pass param as a ClassMetadata, ex. ["discr" => $em->getClassMetadata(SuperUser::class)]
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6864