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

afterFetch wont execute when converting the resultset to array (toArray())

so in my model:

<?phppublic function afterFetch()
{
  $this->booleanField = (bool)$this->booleanField;
}

and this:

<?phpAdministrator::find();

is OK, the "booleanField" is actually converted to boolean. But if I use this:

<?phpAdministrator::find()->toArray();

its again a string, afterFetch didnt fire.



85.5k

https://forum.phalcon.io/discussion/498/afterfetch-

what is the field in you database ? is it int/varchar or bool ?



28.2k
edited Oct '15

BIT. But other INTEGER fields showns as string too



85.5k

can you try with \PDO::ATTR_STRINGIFY_FETCHES false in your DB connection


$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 <<------- TRY WITH THIS ONE
                ]
            ]);

            return $class;
        }, true);


85.5k

you can check here https://php.net/manual/en/pdo.setattribute.php

what PDO::ATTR_STRINGIFY_FETCHES does



28.2k

sorry, it didnt work :S