Skip to content

Commit

Permalink
FIX: Use Hierarchy::prepopulateTreeDataCache() in CMS. (#183)
Browse files Browse the repository at this point in the history
* FIX: Use Hierarchy::prepopulateTreeDataCache() in CMS.

Requires silverstripe/silverstripe-framework#8395
  • Loading branch information
Sam Minnée authored and maxime-rainville committed Sep 25, 2018
1 parent 0dfeb89 commit 05b372c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/Versioned.php
Original file line number Diff line number Diff line change
Expand Up @@ -2311,6 +2311,21 @@ public static function get_versionnumber_by_stage($class, $stage, $id, $cache =
return $version ?: null;
}

/**
* Hook into {@link Hierarchy::prepopulateTreeDataCache}.
*
* @param DataList|array $recordList The list of records to prepopulate caches for. Null for all records.
* @param array $options A map of hints about what should be cached. "numChildrenMethod" and
* "childrenMethod" are allowed keys.
*/
public function onPrepopulateTreeDataCache($recordList = null, array $options = [])
{
$idList = is_array($recordList) ? $recordList :
($recordList instanceof DataList ? $recordList->column('ID') : null);
self::prepopulate_versionnumber_cache($this->owner->baseClass(), Versioned::DRAFT, $idList);
self::prepopulate_versionnumber_cache($this->owner->baseClass(), Versioned::LIVE, $idList);
}

/**
* Pre-populate the cache for Versioned::get_versionnumber_by_stage() for
* a list of record IDs, for more efficient database querying. If $idList
Expand Down
2 changes: 1 addition & 1 deletion tests/php/VersionedNumberCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function testVersionNumberCache($stage, $ID, $cache, $expected)
*/
public function testPrepopulatedVersionNumberCache($stage, $ID, $cache, $expected)
{
Versioned::prepopulate_versionnumber_cache(TestObject::class, $stage);
TestObject::singleton()->onPrepopulateTreeDataCache();
$actual = Versioned::get_versionnumber_by_stage(TestObject::class, $stage, self::${$ID}, $cache);
$this->assertEquals(self::$expectedVersions[$expected], $actual);
}
Expand Down

0 comments on commit 05b372c

Please sign in to comment.