Error with QueryBuilder::setParameter() and associative arrays #7140

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

Originally created by @alexcmdev on GitHub (Apr 26, 2023).

Bug Report

When passing an associative array to a QueryBuilder::setParameter() it triggers the error SQLSTATE[HY093]: Invalid parameter number: parameter was not defined.

Summary

Working with pdo_mysql, I'm triggering an error every time I pass an associative array to a QueryBuilder::setParameter(). I don't know if this is the expected behaviour, but I guees it's not, beacuse if the type is specified as Types::ARRAY it works.
As far as I know it may be a problem of the ParameterTypeInferer::inferType() becuase of this:

if (is_array($value)) {
    return is_int(current($value))
        ? Connection::PARAM_INT_ARRAY
        : Connection::PARAM_STR_ARRAY;
}

Current behavior

The next code will trigger an error SQLSTATE[HY093]: Invalid parameter number: parameter was not defined:

$queryBuilder
    ->andWhere('a.nationality NOT IN (:nationalities)')
    ->setParameter('nationalities', ['ES' => 'ES', 'FR' => 'FR', 'GB' => 'GB']);

whereas this works properly:

$queryBuilder
    ->andWhere('a.nationality NOT IN (:nationalities)')
    ->setParameter('nationalities', ['ES', 'FR', 'GB']);

How to reproduce

Using the pdo_mysql as database driver, add an associative array to setParameter() without defining the type.

Expected behavior

To infer the type automatically when is an associative array.

Originally created by @alexcmdev on GitHub (Apr 26, 2023). ### Bug Report When passing an associative array to a ```QueryBuilder::setParameter()``` it triggers the error ```SQLSTATE[HY093]: Invalid parameter number: parameter was not defined```. #### Summary Working with pdo_mysql, I'm triggering an error every time I pass an associative array to a QueryBuilder::setParameter(). I don't know if this is the expected behaviour, but I guees it's not, beacuse if the type is specified as Types::ARRAY it works. As far as I know it may be a problem of the ParameterTypeInferer::inferType() becuase of this: ``` if (is_array($value)) { return is_int(current($value)) ? Connection::PARAM_INT_ARRAY : Connection::PARAM_STR_ARRAY; } ``` #### Current behavior The next code will trigger an error ```SQLSTATE[HY093]: Invalid parameter number: parameter was not defined```: ``` $queryBuilder ->andWhere('a.nationality NOT IN (:nationalities)') ->setParameter('nationalities', ['ES' => 'ES', 'FR' => 'FR', 'GB' => 'GB']); ``` whereas this works properly: ``` $queryBuilder ->andWhere('a.nationality NOT IN (:nationalities)') ->setParameter('nationalities', ['ES', 'FR', 'GB']); ``` #### How to reproduce Using the pdo_mysql as database driver, add an associative array to setParameter() without defining the type. #### Expected behavior To infer the type automatically when is an associative array.
Author
Owner

@alexcmdev commented on GitHub (Apr 26, 2023):

Maybe is not the type, as setting the type to Types::ARRAY returns the same as findAll.

@alexcmdev commented on GitHub (Apr 26, 2023): Maybe is not the type, as setting the type to Types::ARRAY returns the same as findAll.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7140