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 set default fetch mode?

I work with Phalcon\Db\Adapter\Pdo\Mysql directly. And i want to change default fetch mode.

In PDO i can do this:

$connection = new PDO($options); $connection->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);

For example you can get array this way: $robots = Robots::find()->toArray();

or

$robots = Robots::find( array('hydration' => Resultset::HYDRATE_ARRAYS) );

Check out hydration mode in documentation, there are very detailed information https://docs.phalcon.io/en/latest/reference/models.html#hydration-modes



1.3k
Accepted
answer

You can try this

        $connection = new \Phalcon\Db\Adapter\Pdo\Mysql(array(
            'host' => 'localhost',
            'dbname' => 'phalcon',
            'username' => 'root',
            'password' => '',
            'options' => [PDO::ATTR_CASE => PDO::CASE_LOWER, PDO::ATTR_PERSISTENT => TRUE,],
        ));

Marcio Paiva, that's what I meant. Thanks!

Is Marcio Paiva's way success? I try to set [PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC] but it still return both assoc and array.

the following is

new \Phalcon\Db\Adapter\Pdo\Mysql(array(
        'host' => $config->database->host,
        'username' => $config->database->username,
        'password' => $config->database->password,
        'dbname' => $config->database->dbname,
        'charset' => $config->database->charset,
        'options' =>
            [PDO::ATTR_DEFAULT_FETCH_MODE  =>  PDO::FETCH_ASSOC]
        ));

there is a similar problem solved by overiding the query function.

https://github.com/phalcon/cphalcon/issues/400#event-38423980