如何在symfony2存储库中指定查询结果缓存?

Chr*_*ris 5 caching apc symfony doctrine-orm

我有一个存储库,我正在尝试设置结果缓存.我只能在网上找到一个如何做到这一点的例子......但是当我在我的一个存储库中实现这个例子时,我得到一个错误.我正在使用APC进行缓存,并启用了查询缓存以在我的config.yml文件中使用APC.我已经为APC分配了512M,目前仅使用50M(其中23M用于此单个失败的缓存条目)

这是我的存储库代码:

class AchievementRepository extends EntityRepository
{
    function findAchievementsByCategory($categoryObj)
    {
        $em=$this->getEntityManager()->createQuery("SELECT a FROM FTWGuildBundle:Achievement a where a.category=:category order by a.title")
            ->setParameter('category',$categoryObj);
        $em->useResultCache(true,3600,'findAchievementsByCategory');
        $result=$em->getResult();
        return $result;
    }
}
Run Code Online (Sandbox Code Playgroud)

当执行此操作时,我收到以下错误

Notice: apc_store() [<a href='function.apc-store'>function.apc-store</a>]: &quot;type&quot; returned as member variable from __sleep() but does not exist in /data/www/ftw2/Symfony/vendor/doctrine-common/lib/Doctrine/Common/Cache/ApcCache.php line 80 
Run Code Online (Sandbox Code Playgroud)

当我查看我的apc.php文件以查看缓存的内容时,我在用户缓存部分找到了我的缓存条目,其存储值为

Fatal error:  Nesting level too deep - recursive dependency? in /data/www/localhost/apc.php on line 1000
Run Code Online (Sandbox Code Playgroud)

任何人都可以向我提供一些关于我出错的方向吗?

这个实体中有几列是ManyToOne,我是否需要在此查询中禁用延迟加载才能使其正常工作?如果是这样......怎么样?编辑:我通过添加,fetch ="EAGER"到我的ManyToOne映射启用eager load ...没有苹果:(

编辑#2:答案 - 工人类代码(注意,实体类的所有属性(成就)已更改为受保护)

class AchievementRepository extends EntityRepository
{
    function findAchievementsByCategory($categoryObj)
    {
        $em=$this->getEntityManager()->createQuery("SELECT a FROM FTWGuildBundle:Achievement a where a.category=:category order by a.title")
            ->setParameter('category',$categoryObj);
        $em->useResultCache(true,3600,'findAchievementsByCategory');
        $result=$em->getArrayResult();
        return $result;
    }
}
Run Code Online (Sandbox Code Playgroud)

Pet*_*hof 2

当 Doctrine 缓存一个实体时,它会保存它的序列化状态。问题是,私有属性(由 Doctrine 生成时的默认可见性)无法序列化。要解决此问题,您必须保护实体属性。更多信息:http://doctrine-orm.readthedocs.org/en/2.0.x/reference/working-with-objects.html#merging-entities

另一件事是一个已知问题,(最终)在 Doctrine 2.2 版中修复,该版本将在 Symfony 2.1 中修复。如果由于某种原因无法升级,缓存关联的唯一方法是使用getArrayResult实​​体而不是填充实体。