[5.x] Plans::_getAllPlans() memoization broken when plans table is empty #4285
Open
john-henry wants to merge 1 commit intocraftcms:5.xfrom
Open
[5.x] Plans::_getAllPlans() memoization broken when plans table is empty #4285john-henry wants to merge 1 commit intocraftcms:5.xfrom
john-henry wants to merge 1 commit intocraftcms:5.xfrom
Conversation
Initializes _allPlans array to handle empty results correctly, ensuring consistent memoization behavior.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Commerce version: 5.6.2
Craft version: 5.9.20
Plans::_getAllPlans()uses a null-check to memoize results, but the memoization never activates when thecraft_commerce_planstable is empty. This causes the method to issue a database query on every call within a request, and on every subsequent request, rather than querying once and caching the result.Root cause
When no plans exist,
$this->_allPlansis never assigned, so the===null guard never clears on subsequent calls.Impact
Plugin::getCpNavItem()callsgetAllPlans()on every CP request to decide whether to show the Subscriptions subnav item. On a site with no subscription plans this means a redundantcraft_commerce_plansquery fires on every single CP request. Because Craft's CP element index triggers ~40 AJAX sub-requests per page load, a single page visit produces ~40 identical database queries that should produce zero after the first.Expected behaviour
_getAllPlans()queries the database exactly once per request regardless of whether the plans table is empty or not.Suggested fix
Unconditionally assign
_allPlansbefore the !empty check so an empty result set is also memoized:This is a one-line fix and eliminates the redundant queries entirely.