Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PDO's WHERE IN (?) issue due to unsupported array parameters #69

Closed
franzose opened this issue May 13, 2018 · 3 comments
Closed

PDO's WHERE IN (?) issue due to unsupported array parameters #69

franzose opened this issue May 13, 2018 · 3 comments
Labels

Comments

@franzose
Copy link

franzose commented May 13, 2018

Hi! Many thanks for the library.

Example query:

$factory->select()->from('foo')->where(field('bar')->in([1,2,3,4]));

Generated SQL is:

SELECT * FROM foo WHERE bar IN (?)

Parameters: [1,2,3,4]. The issue is that PDO does not support array parameters like this one, so I suggest to enhance the Latitude\QueryBuilder\Partial\Parameter class to handle field('bar')->in([1,2,3,4]) properly. It could be done like so:

public function __construct($value)
{
    if (\is_bool($value) || $value === null) {
        $this->sql = var_export($value, true);
    } elseif (\is_array($value)) {
        $this->sql = str_repeat('?,', \count($value) - 1) . '?';
        array_push($this->params, ...$value);
    } else {
        $this->params[] = $value;
    }
}

As of now I had to write some functions and classes to override the ‘invalid’ behaviour:

function field($name): CriteriaBuilder
{
    return new CriteriaBuilder(identify($name));
}

function array_param($value): StatementInterface
{
    // custom class with the logic mentioned above
    return isStatement($value) ? $value : new ArrayParameter($value);
}

function array_param_all(array $values): array
{
    return array_map('array_param', $values);
}

function array_listing(array $values, string $separator = ', '): Listing
{
    return new Listing($separator, ...array_param_all($values));
}

I can't say if the change could break backward compatibility but it would be nice to have parameters binding fixed.

I can submit PR if you like.

@shadowhand
Copy link
Owner

Latitude already supports in() with arrays:

$criteria = field('id')->in(...$array);

@shadowhand
Copy link
Owner

Also refer to the php documentation for argument unpacking.

@franzose
Copy link
Author

@shadowhand wow, sorry, my fault...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants