0

I found a question explaining the problem as I have - Symfony2 + Doctrine2 is not caching results of joined entities, but the answer doesn't help, because I'm using Sf 2.3 and Doctrine 2.3.6. Also, instead of APC I use MemCache for query and result caching.

config.yml

doctrine:
    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        auto_mapping: true
        query_cache_driver:
            type: memcache
            host: "%result_memcache_host%"
            port: "%result_memcache_port%"
            instance_class: Memcache
        result_cache_driver:
            type: memcache
            host: "%result_memcache_host%"
            port: "%result_memcache_port%"
            instance_class: Memcache

In controller:

$rs = $this->getEM()
        ->getRepository('MainBundle:Channels')
        ->findByIdFullData($id)
        ->useResultCache(true, 900, 'channel' . $id)
        ->getOneOrNullResult();

In repository:

public function findByIdFullData($id)
{
    $rs = $this
        ->createQueryBuilder('c')
        ->select('c', 'al')
        ->leftJoin('c.audioLanguage', 'al')
        ->where('c.id = :id')
        ->andWhere('c.active = 1')
        ->setParameter('id', $id);

    return $rs->getQuery();
}

audioLanguage is a ManyToMany relation between channels and languages and every time i get a query to DB while trying to access it in twig or by calling getAudioLanguage()

What could be wrong?
Thanks

Community
  • 1
  • 1
Karmalakas
  • 1,143
  • 2
  • 19
  • 39

2 Answers2

0

You're setting the same cache key for every query, remove third parameter from useResultCache. And maybe you should specify Hydration mode in getOneOrNullResult to Doctrine\ORM\AbstractQuery::HYDRATE_OBJECT? Good luck.

  • Sorry for confusion, I use unique names for cache name, just cleared some unnecessary code in a question (edited now) :) Setting hydration mode as You suggest also doesn't change anything. By the way, I'm using Translatable from Doctrine extensions, and I set Object hydration there. – Karmalakas Dec 05 '14 at 08:52
  • Have a look on my other answer: http://stackoverflow.com/questions/27169763/symfony-2-3-gedmo-doctrine-extensions-translatable-caching/27173280#27173280 – Karol Wojciechowski Dec 05 '14 at 09:09
  • I don't think that's related. Even without translatable many-to-many relations aren't cached. – Karmalakas Dec 05 '14 at 10:00
  • Similar resolved problem: http://stackoverflow.com/questions/13336953/doctrine-result-cache-not-caching-a-query-with-a-join – Karol Wojciechowski Dec 05 '14 at 10:24
  • Upgraded Doctrine to 2.3.6, but still the same. Couldn't figure out how to make use of the link You provided. Are You suggesting I should _create my own "result cache"_? – Karmalakas Dec 05 '14 at 11:19
  • I don't know if its related, but maybe. Have look at my answer below. – Karol Wojciechowski Dec 05 '14 at 11:36
0

In our project main project (doctrine 2.4) we have a custom cache tagging feature, and every cached query is handled by this method (it re-sets resultCacheDriver for every query):

public function add($query, $lifetime = null)
    {
        $ownRedis = new RedisCache();
        /** @var $query Query */
        $query->useResultCache(true,$lifetime);
        $redis = $query->getQueryCacheProfile()->getResultCacheDriver();
        $ownRedis->setRedis($redis->getRedis());
        $query->setResultCacheDriver($ownRedis);
        self::$_queries[] = $query;
    }