Problem with INSTANCE OF and array parameter #5021

Closed
opened 2026-01-22 14:56:50 +01:00 by admin · 2 comments
Owner

Originally created by @ghost on GitHub (Feb 25, 2016).

I don't know whether this is me misunderstanding something but if I use an array parameter with INSTANCE OF I have to pass an array of discriminators for the query to work.

The following works:

$qb = $em->createQueryBuilder('e')->where('e INSTANCE OF :instance');
$qb->setParameter('instance', [
    'discriminator_for_entity_class1',
    'discriminator_for_entity_class2',
]);
$result = $qb->getQuery()->execute();

But this example doesn't, it runs, but inserts the class names into the SQL rather than the discriminator:

$qb = $em->createQueryBuilder('e')->where('e INSTANCE OF :instance');
$qb->setParameter('instance', [
    $em->getClassMetadata('Entity\EntityClass1'),
    $em->getClassMetadata('Entity\EntityClass2'),
]);
$result = $qb->getQuery()->execute();

I'd expect the second one to work because if you supply a single parameter it should be a ClassMetadata instance.

Originally created by @ghost on GitHub (Feb 25, 2016). I don't know whether this is me misunderstanding something but if I use an array parameter with INSTANCE OF I have to pass an array of discriminators for the query to work. The following works: ``` $qb = $em->createQueryBuilder('e')->where('e INSTANCE OF :instance'); $qb->setParameter('instance', [ 'discriminator_for_entity_class1', 'discriminator_for_entity_class2', ]); $result = $qb->getQuery()->execute(); ``` But this example doesn't, it runs, but inserts the class names into the SQL rather than the discriminator: ``` $qb = $em->createQueryBuilder('e')->where('e INSTANCE OF :instance'); $qb->setParameter('instance', [ $em->getClassMetadata('Entity\EntityClass1'), $em->getClassMetadata('Entity\EntityClass2'), ]); $result = $qb->getQuery()->execute(); ``` I'd expect the second one to work because if you supply a single parameter it should be a ClassMetadata instance.
admin closed this issue 2026-01-22 14:56:50 +01:00
Author
Owner

@Ocramius commented on GitHub (Feb 25, 2016):

INSTANCE OF accepts only one parameter value. If you need multiple checks, use the OR operator

@Ocramius commented on GitHub (Feb 25, 2016): `INSTANCE OF` accepts only one parameter value. If you need multiple checks, use the `OR` operator
Author
Owner

@ghost commented on GitHub (Feb 25, 2016):

Ok that's great. I was just confused by this http://stackoverflow.com/questions/7504680/doctrine-2-how-to-write-a-dql-select-statement-to-search-some-but-not-all-the which seems to suggest it is possible.

@ghost commented on GitHub (Feb 25, 2016): Ok that's great. I was just confused by this [http://stackoverflow.com/questions/7504680/doctrine-2-how-to-write-a-dql-select-statement-to-search-some-but-not-all-the](url) which seems to suggest it is possible.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5021