title: How to cache a Doctrine 2 query properly on Symfony?
body: <p>I have been reading extensively about Doctrine different options for Caching as well as symfony caching mechanisms:</p>
  
  <p>Symfony Official: <a href="https://symfony.com/doc/4.0/components/cache.html" rel="nofollow noreferrer">https://symfony.com/doc/4.0/components/cache.html</a></p>
  
  <p>Doctrine Official: <a href="https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/caching.html" rel="nofollow noreferrer">https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/caching.html</a></p>
  
  <p>KnP university (very useful as always): <a href="https://knpuniversity.com/screencast/symfony-fundamentals/caching" rel="nofollow noreferrer">https://knpuniversity.com/screencast/symfony-fundamentals/caching</a></p>
  
  <p>Should I use Doctrine Cache or Symfony 4 cache? Which one to choose from?</p>
  
  <p>I have a large amount of data to pull from my database that I’d like to cache (pulling entities with tons of left joins).Those left joins are, for some, updated every hour, every day or every minutes on regular basis though a bot called with a cron job (symfony command).</p>
  
  <pre><code>/**
   * @return Coins[] Returns an array of Crypto objects
   */
  
  public function findOneByTickerRelationnal($ticker)
  {
      $em = $this-&gt;getEntityManager();
      $updatesrepository = $em-&gt;getRepository(Updates::class);
      $updates = $updatesrepository-&gt;findOneBy(['id'=&gt; 1 ]);
  
      // This is where I’ve been doing additional work to limit my left join as much as possible with a ‘with’ on left join
      $recentMarkets = $updates-&gt;getMarket();
      $recentPrices = $updates-&gt;getPrice();
      $recentSources = $updates-&gt;getSources();
  
      $cryptos = $this-&gt;createQueryBuilder('c')
          -&gt;select('partial c.{id, name, ticker}’) //&lt;= use of partial is a plus but you need to know exactly which fields you want
          -&gt;leftJoin('c.prices', 'p','WITH', 'p.last_updated &gt;= :recentPrices')
          -&gt;addSelect('partial p.{id, price_usd, daily_volume_usd, change_1h, change_1d, change_7d, rank}')
          -&gt;leftJoin('c.markets', 'm','WITH', 'm.last_updated &gt;= :recentMarkets')
          -&gt;addSelect('partial m.{id, cur_supply, market_cap, max_supply}')
          -&gt;leftJoin('c.sources', 's','WITH', 's.last_updated &gt;= :recentSources')
          -&gt;addSelect('s')
          -&gt;where('c.ticker = :ticker')
          -&gt;setParameter('recentPrices', $recentPrices)
          -&gt;setParameter('recentMarkets', $recentMarkets)
          -&gt;setParameter('recentSources', $recentSources)
          -&gt;setParameter('ticker', $ticker)
          -&gt;getQuery()
          -&gt;getArrayResult(); //&lt;=Changes everything 
  
      $results = $cryptos[0];
  
      return $results;
  }
  </code></pre>
  
  <p>I’m not sure How to cache this query properly considering the frequent updates.</p>
  
  <p>Thanks</p>