From 93ad0757499f91f63e3540c710c12ddcf816d546 Mon Sep 17 00:00:00 2001 From: blair Date: Thu, 29 Oct 2015 11:45:21 -0600 Subject: [PATCH] Backport of #788 to the 3.3 branch --- .../SeoBundle/Twig/SeoTwigExtension.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Kunstmaan/SeoBundle/Twig/SeoTwigExtension.php b/src/Kunstmaan/SeoBundle/Twig/SeoTwigExtension.php index a288b27993..d9b0074b79 100644 --- a/src/Kunstmaan/SeoBundle/Twig/SeoTwigExtension.php +++ b/src/Kunstmaan/SeoBundle/Twig/SeoTwigExtension.php @@ -30,6 +30,13 @@ class SeoTwigExtension extends Twig_Extension */ private $websiteTitle; + /** + * Saves querying the db multiple times, if you happen to use any of the defined + * functions more than once in your templates + * @var array + */ + private $seoCache = []; + /** * @param EntityManager $em */ @@ -61,7 +68,15 @@ public function getFunctions() */ public function getSeoFor(AbstractPage $entity) { - return $this->em->getRepository('KunstmaanSeoBundle:Seo')->findOrCreateFor($entity); + $key = md5(get_class($entity).$entity->getId()); + + if (!array_key_exists($key, $this->seoCache)) + { + $seo = $this->em->getRepository('KunstmaanSeoBundle:Seo')->findOrCreateFor($entity); + $this->seoCache[$key] = $seo; + } + + return $this->seoCache[$key]; } /**