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

how to bind where in ?

eg; I won't this $array = ['1','2']; //array $robots = Robots::find( [ 'letter IN ({letter:array})', 'bind' => [ 'letter' => $array ] ] );

because versions of the problem

I want this $array = ['1','2']; //array $robots = Robots::find( [ 'letter IN (:letter:)', 'bind' => [ 'letter' => implode(',', $array) ] ] );



85.5k
edited Nov '16

pdo doesnt support this you have to implement it on your own.


$conditions = "";
$bind = [];

foreach ($letters as $idx => $letter ){
    $conditions.=" letter = ?".$idx." AND ";
    $bind[$idx] => $letter;
}

$conditions  = rtrim($conditions, " AND");

Model::find([
    "conditions" => $conditions,
    "bind" => $bind
]);

Or somethign like that

you can use whereIn with query builder ( I think ).

https://docs.phalcon.io/en/3.0.0/api/Phalcon_Mvc_Model_Query_Builder.html search for inWhere

You'll have to use the builder:

class SomeController extends \Phalcon\Mvc\Controller
{
    public function indexAction() {
        $myModel = $this->getModelsManager()->createBuilder()
            ->addFrom('MyModel','m')
            ->inWhere('m.type',['foo','bar','baz'])
            ->getQuery()
            ->execute()
            ->getFirst();
        var_dump($myModel->toArray());
    }
}