We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

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.

Something wrong with Models Cache and custom PDO options

Hi all.

I have mysql table catalog(id, title, status) and simple application with one model

$di = new \Phalcon\DI\FactoryDefault();

$di->set(
    'modelsMetadata', function() {
        return new \Phalcon\Mvc\Model\MetaData\Apc(
            array(
                "lifetime" => 86400,
                "prefix" => "metadata"
            )
        );
    }
);

$di->set(
    'modelsCache', function() {
        $frontCache = new \Phalcon\Cache\Frontend\Data(
            array(
                "lifetime" => 600,
                "prefix"   => "cache"
            )
        );
        return new \Phalcon\Cache\Backend\Apc($frontCache);
    }
);

$di->set(
    'db', function() {
        return new \Phalcon\Db\Adapter\Pdo\Mysql(
            array(
                'host' => 'localhost',
                'username' => 'login',
                'password' => 'pass',
                'dbname' => 'db',
                'charset' => 'utf8',
                'options' => array(
                    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
                    PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
                )
            )
        );
    }
);

class Catalog extends \Phalcon\Mvc\Model
{

}
echo 'example page' . PHP_EOL;
$categories = Catalog::find(array("status = 1", 'cache' => array('key' => 'my_cache_key', 'lifetime' => 120)));
echo 'total count: ' . count($categories) . PHP_EOL;
foreach ($categories as $category) {
    print_r($category->toArray());
}

when i run it first (with empty cache) i see: "example page total count: XX" with empty foreach cycle, but next runs everything works good.

After several debug time i've found the reason. it's

PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,

Can anybody tell me why and how to fix it?

ps: PHP Version 5.3.10-1ubuntu3.8/phalcon 1.2.3/ pdo_mysql 5.5.32



98.9k
Accepted
answer

The ORM requires to use PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC to work properly, if you don't want to use the ORM you can freely change the global fetch mode without any collateral effects.



754

ok, thanks