Documentation for 'IN' with query builder is misleading #5626

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

Originally created by @gbirke on GitHub (Aug 2, 2017).

The documentation for the query builder and building IN and NOT IN expression says

// Make sure that you do NOT use something similar to $qb->expr()->in('value', array('stringvalue')) as this will cause Doctrine to throw an Exception.
// Instead, use $qb->expr()->in('value', array('?1')) and bind your parameter to ?1 (see section above)

Relying on that comment I tried code like this:

$qb->where( $qb->expr()->in( 'd.status', [':statusA', ':statusB'] ) )
  ->addParameter( 'statusA', 'A' )
  ->addParameter( 'statusB', 'B' )

This lead to the error message Too many parameters: the query defines 0 parameters and you bound 2

When I specified the strings directly, the query worked as expected:

$qb->where( $qb->expr()->in( 'd.status', ['A', 'B'] ) )

Looking at the code in the expression builder it looks like my strings will be quoted correctly.

Is the comment in the documentation still relevant? Maybe it hints at a different use case (e.g. subqueries)?
I'm not sure how the text could be improved.

Originally created by @gbirke on GitHub (Aug 2, 2017). The [documentation for the query builder](https://github.com/doctrine/doctrine2/blob/master/docs/en/reference/query-builder.rst) and building `IN` and `NOT IN` expression says > // Make sure that you do NOT use something similar to $qb->expr()->in('value', array('stringvalue')) as this will cause Doctrine to throw an Exception. // Instead, use $qb->expr()->in('value', array('?1')) and bind your parameter to ?1 (see section above) Relying on that comment I tried code like this: ```php $qb->where( $qb->expr()->in( 'd.status', [':statusA', ':statusB'] ) ) ->addParameter( 'statusA', 'A' ) ->addParameter( 'statusB', 'B' ) ``` This lead to the error message ` Too many parameters: the query defines 0 parameters and you bound 2` When I specified the strings directly, the query worked as expected: ```php $qb->where( $qb->expr()->in( 'd.status', ['A', 'B'] ) ) ``` Looking at the code in the expression builder it looks like my strings will be quoted correctly. Is the comment in the documentation still relevant? Maybe it hints at a different use case (e.g. subqueries)? I'm not sure how the text could be improved.
admin added the DocumentationQuestion labels 2026-01-22 15:13:13 +01:00
Author
Owner

@Dormilich commented on GitHub (Dec 9, 2024):

Expr::in() will quote all values contained in the second argument if it is iterable (i.e. when it assumes it is a list of values for the IN() clause). For binding parameters you just pass in the placeholder as is (e.g. $expr->in('e.id', '?1')).

Insofar the documentaion is incorrect when it comes to using placeholders.

@Dormilich commented on GitHub (Dec 9, 2024): `Expr::in()` will quote all values contained in the second argument if it is iterable (i.e. when it assumes it is a list of values for the IN() clause). For binding parameters you just pass in the placeholder as is (e.g. `$expr->in('e.id', '?1')`). Insofar the documentaion is incorrect when it comes to using placeholders.
Author
Owner

@greg0ire commented on GitHub (Dec 9, 2024):

Since 4adc289596, there even is a test showing that it's fine to use $qb->expr()->in('value', array('stringvalue'))

@Dormilich can you send a PR to fix the docs?

@greg0ire commented on GitHub (Dec 9, 2024): Since 4adc289596ef640d5c8c605e2aea5674e6502b46, there even is a test showing that it's fine to use `$qb->expr()->in('value', array('stringvalue'))` @Dormilich can you send a PR to fix the docs?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5626