Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Binding arrays (2.0.4+) in PHQL

https://blog.phalconphp.com/post/phalcon-2-0-4-released

  • Introduced new placeholders in PHQL enclosed in brackets that allow to set the type: {name:str} or {names:array}
  • Now you can bind arrays in bound parameters in PHQL

I'm already doing lots of magic to bind arrays with PHQL and PDO but I read a while back that PHQL supports direct array binding now and I figured that I could go full Phalcon and cut down on some tricky custom code. I'm already checking that the arrays have scalar values (just string and integer in my case). I'm taking an approach to move from numbered values from my layer above PHQL and now I'm trying to use named values.

I've tried the following forms :PARAM0: and {PARAM0:array} with an array bound to them.

I'm also sending in a bind type array for the LIMIT clause (since its required) and I'm wondering if it might conflict. Some weeks ago I made a quick attempt to get this new bind syntax {name:type} working for LIMIT but it didn't seem to work.

Is anyone using this new feature yet?



46.8k
edited Aug '15

I'm getting: Syntax error, unexpected token STRING PLACEHOLDER(PARAM1), near to ... when I use it with a bound array. When I hack the bound variable PARAM1 that point to be an integer (also changing IN to =) then it works.

Let me ask more simply: Is anyone binding arrays?



46.8k

When I bind the type to an array with {PARAM1:array} then I get the following error message: Syntax error, unexpected token UNKNOWN(PARAM1:array), near to ....



46.8k
Accepted
answer
edited Aug '15

As Andres posted on the github issue the binding reference must be wrapped in parenthesis for an array.

$phql = "SELECT * FROM Some\\Robots WHERE id IN ({PARAM0:array})";
$robots = $modelsManager->executeQuery($phql, [
    'PARAM0' => [1, 2, 3]
]);