Created
October 24, 2018 01:14
-
-
Save sminnee/fa2c1ec5de5f87afe89d45813b99116d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| commit 3ea3015210ca61dc451ea217f72d55444b32f2f2 | |
| Author: Sam Minnee <[email protected]> | |
| Date: Wed Oct 24 14:14:03 2018 +1300 | |
| Fix - prebuild the list of taxonomy terms by parent id | |
| diff --git a/src/Extension/GazetteTaxonomyTermExtension.php b/src/Extension/GazetteTaxonomyTermExtension.php | |
| index 291fdba..1952c6a 100644 | |
| --- a/src/Extension/GazetteTaxonomyTermExtension.php | |
| +++ b/src/Extension/GazetteTaxonomyTermExtension.php | |
| @@ -295,33 +295,41 @@ class GazetteTaxonomyTermExtension extends DataExtension | |
| */ | |
| public function getCachedChildren() | |
| { | |
| - $children = new ArrayList(); | |
| - foreach (self::getCachedTaxonomyTerms() as $taxonomyTerm) { | |
| - if ($taxonomyTerm->ID == $this->owner->ID) { | |
| - continue; | |
| - } | |
| - if ($taxonomyTerm->ParentID == $this->owner->ID) { | |
| - $children->push($taxonomyTerm); | |
| - } | |
| + self::warmTaxonomyCache(); | |
| + | |
| + if (isset(self::$taxonomyTerms['byParentID'][$this->owner->ID])) { | |
| + return new ArrayList(self::$taxonomyTerms['byParentID'][$this->owner->ID]); | |
| + } else { | |
| + return new ArrayList(); | |
| } | |
| - return $children; | |
| } | |
| /** | |
| - * Get all data from the TaxonomyTerms table, keyed by name, cached in memory for the current pageview | |
| - * Currently currently there's only 225 records in the database | |
| + * Gets a taxonomy term by name. | |
| + * | |
| + * @param string $name The name of the taxonomy term to find | |
| * | |
| - * @return array|null | |
| + * @return TaxonomyTerm The first taxonomy term that matches the given | |
| + * name. | |
| */ | |
| - protected static function getCachedTaxonomyTerms() | |
| + public static function getTaxonomyByName($name) | |
| { | |
| + self::warmTaxonomyCache(); | |
| + | |
| + return isset(self::$taxonomyTerms['byName'][$name]) ? self::$taxonomyTerms['byName'][$name] : null; | |
| + } | |
| + | |
| + protected static function warmTaxonomyCache() { | |
| if (self::$taxonomyTerms == null) { | |
| - self::$taxonomyTerms = []; | |
| + self::$taxonomyTerms = [ 'byName' => [], 'byParentID' => [] ]; | |
| foreach (TaxonomyTerm::get() as $taxonomyTerm) { | |
| - self::$taxonomyTerms[$taxonomyTerm->Name] = $taxonomyTerm; | |
| + self::$taxonomyTerms['byName'][$taxonomyTerm->Name] = $taxonomyTerm; | |
| + | |
| + if ($taxonomyTerm->ParentID != $taxonomyTerm->ID) { | |
| + self::$taxonomyTerms['byParentID'][$taxonomyTerm->ParentID][] = $taxonomyTerm; | |
| + } | |
| } | |
| } | |
| - return self::$taxonomyTerms; | |
| } | |
| /** | |
| @@ -383,20 +391,6 @@ class GazetteTaxonomyTermExtension extends DataExtension | |
| } | |
| /** | |
| - * Gets a taxonomy term by name. | |
| - * | |
| - * @param string $name The name of the taxonomy term to find | |
| - * | |
| - * @return TaxonomyTerm The first taxonomy term that matches the given | |
| - * name. | |
| - */ | |
| - public static function getTaxonomyByName($name) | |
| - { | |
| - $taxonomyTerms = self::getCachedTaxonomyTerms(); | |
| - return isset($taxonomyTerms[$name]) ? $taxonomyTerms[$name] : null; | |
| - } | |
| - | |
| - /** | |
| * Gets a list of taxonomy terms from a list of slugs or IDs. | |
| * | |
| * @param array $allSlugs List of slugs or ID values. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment