We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Phalcon PHP bind condition array first index is ignored

I'm using Phalcon 3. I create a web service who take in parameter a list of ids then I check if theses ids exist (with IN condition) in my database then I update them.

They are all updated except one who is the first of my ids list.

There is a problem with my bind condition ? I can confirm than it is present in the $ids array. I have the problem too if I have just 1 id inside my list, this id will be ignored.

public function myAction() {
    $this->view->disable();
    $tools = new Tools();
    $api_key = $this->request->getQuery('apiKey');
    $ids = explode(",", $this->request->getPut('ids'));

    ...

    $objects = Objects::find(array(
        "conditions" => "id IN ({ids:array})",
        "bind" => array("ids" => $ids)
    ));

    if (count($objects) > 0) {
        foreach ($objects as $obj) {
            ...
        }
    }
    return $tools->JSONBuilder(array("success" => "message"), 200);
}

being numeric ids you could use array-intinstead of array

$ids = array_map(function($id){ return abs(intval($id));},$ids); // ids transformed into abs ints
$ids = array_filter($ids); // clean not > 0 ids
 $objects = Objects::find(array(
        "conditions" => "id IN ({ids:array-int})",
        "bind" => array("ids" => $ids)
    ));

The rest looks fine, good luck

edited Nov '18

I resolve my problem by adding json_decode();

$ids = json_decode($this->request->getPut('ids'));

But I don't understand why it works

Dump your $ids variable with explode() and with json_decode() and show us both results.

When binding the {variable:array} the array keys should start from 0. There is an issue on github about this, but people say its intended.

Here is what I do when my array has variable keys:

$builder->andWhere('cruise.id IN({similarCruiseIds:array})', ['similarCruiseIds' => array_values($ids)]);