Lazy loading using hydration cache

$queryBuilder = $this->getEntityManager()->createQueryBuilder()
    ->from('ShopHqCartBundle:PromotionalDiscount', 'pd')
    ->leftJoin('pd.requiredSkus', 'pdrs')
    ->andWhere('pd.id = :id')
    ->select('pd', 'pdrs');
$query = $queryBuilder->getQuery();

$cache = $this->getEntityManager()->getConfiguration()->getResultCacheImpl();
$hydrationCacheProfile = new QueryCacheProfile(60 * 60 * 12, $cacheKeyHydrated, $cache);

$result = $query
  ->useQueryCache(true)
  ->useResultCache(true, 60 * 60 * 12, $cacheKey)
  ->setHydrationCacheProfile($hydrationCacheProfile)
  ->getResult();

The first time this is done, I return a managed entity, and I can be lazy to load other relationships. When you return the result from the hydration cache, it is not a managed entity, and therefore lazy loading does not work. Is there something I can do to save the hydration cache and get a lazy load to work with the result of the hydration cache?

+4
source share

Source: https://habr.com/ru/post/1568040/


All Articles