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.

Casting field values after fetching records using query builder

I know that I can do "Model::setUp(["castOnHydrate" => true])" and doing $myModel->toArray() will return properly typed field values.

Now the issue is that even after doing Model::setUp(), following code still returns alll the values as strings:

$entities = $this->modelsManager->createBuilder()
            ->from(['mm' => 'MyModel'])
            ->getQuery()->execute();

print_r($entities->toArray());

Is there a simple way to make sure the values are properly typed in this case too?

You sure you have mysqlnd driver ?



64.0k
Accepted
answer
edited Aug '16

I cant find the old thread about this ...

$this->di->set('db', function() use ($config) {

            $class = new \Phalcon\Db\Adapter\Pdo\Mysql([
                'host' => $config->database->host,
                'username' => $config->database->username,
                'password' => $config->database->password,
                'dbname' => $config->database->dbname,
                'port' => '3306',
                'charset' => 'utf8',
                "options"  => [
                    \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
                    \PDO::ATTR_PERSISTENT => true,
                    \PDO::ATTR_EMULATE_PREPARES => false,
                    \PDO::ATTR_DEFAULT_FETCH_MODE  =>  \PDO::FETCH_ASSOC,
                    \PDO::ATTR_STRINGIFY_FETCHES => false //<---------------------------
                ]
            ]);

            return $class;
        }, true);

Thank you, lzo :)