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

Can I bind an array to an IN() condition?: $connection->fetchAll()

Can I bind an array to an IN() condition?

$database = [
        'adapter' => 'Postgresql',
        'host' => 'localhost',
        'username' => 'xerron',
        'password' => 'root',
        'dbname' => 'db_drupal'
    ];

    $adapter = $database['adapter'];
    unset($database['adapter']);

    $class = 'Phalcon\Db\Adapter\Pdo\\' . $adapter;

    $connection = new $class($database);

    $list = false;
    try {
        $names = ['user.role.administrator', 'user.role.anonymous'];
        $sql = 'SELECT name, data FROM config WHERE collection = :collection AND name IN ( :names )';
        $list = $connection->fetchAll(
            $sql,
            \Phalcon\Db::FETCH_ASSOC,
            [
                'collection' => '',
                'names' => implode(',', $names)   // <-- this not work!!!
            ]
        );
        foreach ($list as &$data) {
            $data = unserialize(stream_get_contents($data['data']));
        }
    } catch (\Exception $e) {
        // If we attempt a read without actually having the database or the table
        // available, just return FALSE so the caller can handle it.
    }

    d($list); // kint debug

I found a solution to use this:

SELECT name, data FROM config WHERE collection = :collection AND name IN ( :names[] )