Skip to content

Commit 400c980

Browse files
committed
Merge pull request #1254 from zonky2
Fixes #1253 - sorting by "sorting"-field was broken due to caching.
2 parents 5eb78e2 + fca395a commit 400c980

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/MetaModels/MetaModel.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -730,27 +730,37 @@ public function getIdsFromFilter($objFilter, $strSortBy = '', $intOffset = 0, $i
730730
} elseif ('id' === $strSortBy) {
731731
asort($arrFilteredIds);
732732
} elseif (in_array($strSortBy, array('pid', 'tstamp', 'sorting'))) {
733-
// Check existing ids.
734-
if (array_intersect($arrFilteredIds, $this->existingIds) == $arrFilteredIds) {
735-
return $arrFilteredIds;
733+
// Build the right key for the cache.
734+
$sortKey = \sprintf('%s-%s', $strSortBy, \strtolower($strSortOrder));
735+
// Used the cached ID list, and make a list of wanted ID's with the sorting of the cache.
736+
$cacheResult = array_intersect((array) $this->existingIds[$sortKey], $arrFilteredIds);
737+
// Check if we have all ID's or if we have one missing, now we are using the order of the MM Filter.
738+
if (array_intersect($arrFilteredIds, $cacheResult) === $arrFilteredIds) {
739+
return $cacheResult;
736740
}
737-
741+
742+
// Merge the already known and the new one.
743+
$fullIdList = array_merge((array) $this->existingIds[$sortKey], $arrFilteredIds);
744+
$fullIdList = \array_keys(\array_flip($fullIdList));
745+
738746
// Sort by database values.
739-
$arrFilteredIds = $this
747+
$arrSortedFilteredIds = $this
740748
->getDatabase()
741749
->prepare(
742750
sprintf(
743751
'SELECT id FROM %s WHERE id IN(%s) ORDER BY %s %s',
744752
$this->getTableName(),
745-
$this->buildDatabaseParameterList($arrFilteredIds),
753+
\implode(', ', \array_fill(0, count($fullIdList), '?')),
746754
$strSortBy,
747755
$strSortOrder
748756
)
749757
)
750-
->execute($arrFilteredIds)
758+
->execute($fullIdList)
751759
->fetchEach('id');
752760

753-
$this->existingIds = array_merge($this->existingIds, $arrFilteredIds);
761+
// Add the new sorted Id's to the cache and use only the wanted.
762+
$this->existingIds[$sortKey] = $arrSortedFilteredIds;
763+
$arrFilteredIds = array_intersect($arrSortedFilteredIds, $arrFilteredIds);
754764
} elseif ($strSortBy == 'random') {
755765
shuffle($arrFilteredIds);
756766
}

0 commit comments

Comments
 (0)