ZF-6352: Zend_Cache::save() encoding problem

Issue Type: Bug Created: 2009-04-20T01:43:46.000+0000 Last Updated: 2009-05-01T00:32:28.000+0000 Status: Resolved Fix version(s): Reporter: Jiri Helmich (teuzz) Assignee: Fabien MARTY (fab) Tags: - Zend_Cache

Related issues: Attachments:


When using Zend_Cache::start() to save a paginator object, encoding of stored db query result gets broken (originally UTF-8).

if (!$this->view->frontPageArticles = $this->_cache->load('frontPageArticles')) { $content = new CMS_Model_Content(); $this->view->frontPageArticles = $content->getFrontpageContent($this->_params['page']); //gets Zend_Paginator object $this->_cache->save($this->view->frontPageArticles,'frontPageArticles');

When the last line of code is commented (and cache purged), everything is ok (chars like ěščř... are displayed correctly).

ZF 1.8


Posted by Fabien MARTY (fab) on 2009-04-20T10:51:00.000+0000

please provide a little piece of code to reproduce the problem because there is already a unit test about UTF8 into cache records and there is no problem about it

I close this issue because I think that you have an HTTP header problem or something like that. With default configuration, Zend_Cache_Frontend_Page does not save the "content-type" http header (but there are some options to do that) and IMHO your problem is here.

If I'm wrong, please reopen this issue with a little piece of code to reproduce your UTF8 problem.


Posted by Jiri Helmich (teuzz) on 2009-04-20T14:56:47.000+0000

Well, as you can see here: , there isn't a problem with whole page but only with the content touched by Zend_Cache (the main article, which is, I think, clear).

The code:

  1. Bootstrap:

$cache = Zend_Cache::factory( 'Core', 'File', array( 'lifetime' => (15*60), 'automatic_serialization' => true, 'caching' => CACHE_ENABLED ), array( 'cache_dir' => '../store/cache/' ) ); Zend_Registry::set('MainCache',$cache);

  1. Controller:

private $_cache;

public function init()
    $this->_params = $this->getRequest()->getUserParams();
    $this->_cache = Zend_Registry::get('MainCache');

public function indexAction()

    if (!$this->view->frontPageArticles = $this->_cache->load('frontPageArticles'))
        $content = new CMS_DbTable_Content();
        $this->view->frontPageArticles = $content->getFrontpageContent($this->_params['page']);
  1. CMS_DbTable_Content

public function getFrontpageContent($page = 0) {

        $select = $this->select();

                    'content_frontpage.content_id =',
                ->order('content.created DESC')
                ->where('content.state = 1');

                $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbTableSelect($select));

        return $paginator;


So as you can see, I do not cache whole page but only the returned paginator object - if I'm right, the query is proccessed after calling getCurrentItems(), so I do not really cache any data from database, so that's more weird that the data I get after calling getCurrentItems are broken.

And I did not forget to call set names utf-8 :-)

Posted by Fabien MARTY (fab) on 2009-04-21T12:30:58.000+0000

I don't know. Your code is too complex. I can't reproduce this on my linux box.

Can you try a unit test : - you save UTF8 chars into a cache record - you read this cache record

then you compare these two strings

Posted by TT (tt) on 2009-04-29T18:54:18.000+0000

I can confirm this issue. If you save a Zend_Paginator object in the cache (I only tested file cache), the result that is loaded from the cache has messed up UTF-8 encoded strings.

<pre class="literal"> 
$frontendOptions = array(
   'caching' => true,
   'lifetime' => 7200, 
   'automatic_serialization' => true

$backendOptions = array(
    'cache_dir' => 'cache/' 

$cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);

//Zend_Db_Table_Select object
$select = $this->select();

$paginator = Zend_Paginator::factory($select );

$cache->save($paginator, 'a_cache_id');

$cachedPaginator = $cache->load('a_cache_id');

The UTF-8 chars in $paginator are fine, in $cachedPaginator they are messed up.

Posted by Fabien MARTY (fab) on 2009-04-30T10:58:17.000+0000

I suppose this is a Zend_Paginator or a Zend_Db_Table_Select or a serialize() issue.

Can you try in your configuration to store UTF8 chars (with automatic_serialization = false) and without other objects (only UTF8 chars !) and to retrieve them from cache ?


Posted by Fabien MARTY (fab) on 2009-04-30T11:00:19.000+0000

after reading comments about utf8

I think it's a unserialize() issue

please do the little test I propose in my last comment

and do it also with automatic_serialization = true

Posted by TT (tt) on 2009-04-30T19:16:41.000+0000

I did some more testing and it seems to have to do with serialization, probably the Zend_Db_Table_Select and not the Zend_Paginator. I only tested with Zend_Paginator using a Zend_Db_Table_Select though.

This is strange:

Apparently serializing a Zend_Paginator object that includes a result of a Zend_Db_Table_Select breaks the UTF-8.

<pre class="literal"> 
$paginatorObject = serialize($paginatorObject );    
$paginatorObject = unserialize($paginatorObject );

//broken UTF-8
foreach ($paginatorObject as $result) {

Now the funny part. If we iterate over the paginator result right before serializing it, the UTF-8 will NOT be broken!

<pre class="literal"> 
//itareate to preserve UTF-8
foreach ($paginatorObject as $result) {

$paginatorObject = serialize($paginatorObject );    
$paginatorObject = unserialize($paginatorObject );

//working UTF-8
foreach ($paginatorObject as $result) {

Basically this is my dirty quick fix for now. Right before I save the paginator object in the cache, I iterate over it once.

Posted by Fabien MARTY (fab) on 2009-05-01T00:32:16.000+0000

So, it is not a Zend_Cache issue.

Maybe open a new one for Zend_Paginator ?

Maybe, try with the latest version of PHP before

Have you found an issue?

See the Overview section for more details.


© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.