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.



64.3k

https://forum.phalconphp.com/discussion/498/afterfetch-

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



12.2k
edited Oct '15

BIT. But other INTEGER fields showns as string too



64.3k

can you try with \PDO::ATTRSTRINGIFYFETCHES 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);


64.3k

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

what PDO::ATTR_STRINGIFY_FETCHES does



12.2k

sorry, it didnt work :S