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

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 ?



85.5k
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 :)