dongtuan1980 2014-08-22 20:08
浏览 62

序列化Doctrine Paginator用于缓存目的

So I had a complex Entity with a few sub-entities managed by OneToManys and ManyToManys.

Using the basic setMaxResults() and setFirstResult() methods were returning less than the desired number of results (when executing the runnable query, I get back my set limit of results, but there are repeat records which Doctrine then trims down and returns the unique records only). Looking through stackoverflow I came across the Paginator Doctrine Tool class.

The issue that I have, is that I need to cache the results. I use Doctrine's ApcCache interface, but when I try to save it to the cache, I get back Exception: Serialization of 'Closure' is not allowed. But the Paginator class doesn't seem to allow me to acquire the the results of the query (just the 10). And obviously when I do this:

$results = new Paginator($query, $fetchJoin = true);
return $results->getQuery()->getResult();

Im back to my original problem of getting less than my desired limit of records (as now we're going back to the original method of getting results)

I just want the array of results, so I may serialize and cache it. What's the best approach to doing that?

Thanks


EDIT

Right now, I am adding a foreach loop and collecting the records into an array. Which seems to work, but I wonder if theres a better way of handling this, than adding another loop in each of my Repository methods...

        $results = new Paginator($query, $fetchJoin = true);

        $return = [];
        foreach($results as $result) {
            $return[] = $result;
        }

        $cache->save($cacheKey, $return, 300);

        return $return;
  • 写回答

1条回答 默认 最新

  • dongying6179 2014-08-23 22:11
    关注

    Try iterator_to_array(), Doctrine Paginator implements the traversable interface http://php.net/manual/en/function.iterator-to-array.php

    评论

报告相同问题?

悬赏问题

  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类