Skip to content

Commit

Permalink
ENH Avoid running queries to retrieve versions on unsaved object (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime Rainville authored Feb 16, 2022
1 parent 81a3064 commit 10d0871
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Versioned.php
Original file line number Diff line number Diff line change
Expand Up @@ -2100,11 +2100,18 @@ public function VersionsList()
*/
public function allVersions($filter = "", $sort = "", $limit = "", $join = "", $having = "")
{
/** @var DataObject $owner */
$owner = $this->owner;

// When an object is not yet in the Database, we can't get its versions
if (!$owner->isInDB()) {
return ArrayList::create();
}

// Make sure the table names are not postfixed (e.g. _Live)
$oldMode = static::get_reading_mode();
static::set_stage(static::DRAFT);

$owner = $this->owner;
$list = DataObject::get(DataObject::getSchema()->baseDataClass($owner), $filter, $sort, $join, $limit);
if ($having) {
// @todo - This method doesn't exist on DataList
Expand Down
9 changes: 9 additions & 0 deletions tests/php/VersionedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,15 @@ public function testAllVersions()
$this->assertEquals($extraFields, ['2005', '2007', '2009'], 'Additional version fields returned');
}

public function testAllVersionsOnUnsavedRecord()
{
$newPage = new VersionedTest\Subclass();
$this->assertCount(0, $newPage->allVersions(), 'No versions on unsaved record');

$singleton = VersionedTest\Subclass::singleton();
$this->assertCount(0, $singleton->allVersions(), 'No versions for singleton');
}

public function testArchiveRelatedDataWithoutVersioned()
{
DBDatetime::set_mock_now('2009-01-01 00:00:00');
Expand Down

0 comments on commit 10d0871

Please sign in to comment.