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

Phalcon Resultset incorrect data types

Hi,

I have a strange behaviuor with Phalcon result set. i am using version 2.0. I have a table called user with primary key _id defined as BIGINT, name as varchar, status as INT. When i query

<?php
$user = User::findFirt();
var_dump($user->toArray());
array(3) {
    ["_id"]=>
    string(1) "2"
    ["name"]=>
    string(5) "admin"
    ["status"]=>
    string(1) "1"
    }

The expected result is _id & status is integer.

Appreciate help. Thanks in advance.

edited Aug '16

I think this is default behaviour of many ORM's, atleast it is like that in many other popular frameworks I've worked with (if i remember well).

You can do the following if you really need integers. In your Model:

public function afterFetch()
{
    $this->_id = (int) $this->_id;
    $this->status = (int) $this->status;
} 


85.5k
edited Aug '16

I use this

$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  // <----------------------------------------
                ]
            ]);

Nice one @Izo, didn't know that. Updating my bootrsap. Thanks ! : )

Also you need to use mysqlnd driver.



2.5k

Hi, thanks for your help. Izo, it didn't worked as expected. I implemented afterFetch().