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.

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::ATTRDEFAULTFETCHMODE, PDO::FETCHOBJ);

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 http://docs.phalconphp.com/en/latest/reference/models.html#hydration-modes



780
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::ATTRDEFAULTFETCHMODE => PDO::FETCHASSOC] 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