$expr->in(...) does not have the same behavior than ->where('foo IN ...')->setParameter(...) #6614

Open
opened 2026-01-22 15:35:42 +01:00 by admin · 2 comments
Owner

Originally created by @VincentLanglet on GitHub (Jan 22, 2021).

Let's say I have an entity Foo and an entity Bar.
Foo::getBar returns an ArrayCollection of Bar.

When writing

->andWhere('foo.bar IN (:bars)')
->setParameter('bars', $myFoo->getBars()->toArray())

the query will be

foo.bar IN (id1, id2, ...)

When writing

->andWhere($query->expr()->in('foo.bar', $myFoo->getBars()->toArray()));

the query will be instead

foo.bar IN (name1, name2, ...)

where name1, name2 are the values returned by Bar::__toString().

I think this behavior misleading. Would it be possible to get some consistency ?
I'm afraid it required unfortunately a BC-break :(

Originally created by @VincentLanglet on GitHub (Jan 22, 2021). Let's say I have an entity Foo and an entity Bar. `Foo::getBar` returns an ArrayCollection of Bar. When writing ``` ->andWhere('foo.bar IN (:bars)') ->setParameter('bars', $myFoo->getBars()->toArray()) ``` the query will be ``` foo.bar IN (id1, id2, ...) ``` When writing ``` ->andWhere($query->expr()->in('foo.bar', $myFoo->getBars()->toArray())); ``` the query will be instead ``` foo.bar IN (name1, name2, ...) ``` where `name1`, `name2` are the values returned by `Bar::__toString()`. I think this behavior misleading. Would it be possible to get some consistency ? I'm afraid it required unfortunately a BC-break :(
admin added the Improvement label 2026-01-22 15:35:42 +01:00
Author
Owner

@ShookTea commented on GitHub (Jan 22, 2021):

AFAIK Expr works on DBAL side of a Doctrine and you're trying to apply ORM elements to it. You're basically mixing two different ways of asking database for something ;)

Try this instead, if you really need to use Expr:

$bars = $myFoo->getBars()->toArray();
$barIds = [];

foreach ($bars as $bar) {
    $barIds[] = $bar->getId();
}
// ...
->andWhere($query->expr()->in('foo.bar', $barIds));
@ShookTea commented on GitHub (Jan 22, 2021): AFAIK `Expr` works on DBAL side of a Doctrine and you're trying to apply ORM elements to it. You're basically mixing two different ways of asking database for something ;) Try this instead, if you really need to use Expr: ```php $bars = $myFoo->getBars()->toArray(); $barIds = []; foreach ($bars as $bar) { $barIds[] = $bar->getId(); } // ... ->andWhere($query->expr()->in('foo.bar', $barIds)); ```
Author
Owner

@VincentLanglet commented on GitHub (Jan 22, 2021):

Try this instead, if you really need to use Expr:

$bars = $myFoo->getBars()->toArray();
$barIds = [];

foreach ($bars as $bar) {
    $barIds[] = $bar->getId();
}
// ...
->andWhere($query->expr()->in('foo.bar', $barIds));

Yes, I agree I can transform my array to id instead.
And I don't need to use Expr.

But some dev in my team pref to use expr, some others prefer to write ('foo.bar IN (:bars). And having them writing something that looks like similar but which lead to different result is not helping them.

It's the same that

->andWhere('foo.bar IN (:bars)')
->setParameter('bars', [])

is returning no result and

->andWhere($query->expr()->in('foo.bar', []));

is throwing an exception.

I find these inconsistency tricky.

@VincentLanglet commented on GitHub (Jan 22, 2021): > Try this instead, if you really need to use Expr: > > ``` > $bars = $myFoo->getBars()->toArray(); > $barIds = []; > > foreach ($bars as $bar) { > $barIds[] = $bar->getId(); > } > // ... > ->andWhere($query->expr()->in('foo.bar', $barIds)); > ``` Yes, I agree I can transform my array to id instead. And I don't need to use Expr. But some dev in my team pref to use `expr`, some others prefer to write `('foo.bar IN (:bars)`. And having them writing something that looks like similar but which lead to different result is not helping them. It's the same that ``` ->andWhere('foo.bar IN (:bars)') ->setParameter('bars', []) ``` is returning no result and ``` ->andWhere($query->expr()->in('foo.bar', [])); ``` is throwing an exception. I find these inconsistency tricky.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6614