diff --git a/src/Versioned.php b/src/Versioned.php index 948dce41..087fc298 100644 --- a/src/Versioned.php +++ b/src/Versioned.php @@ -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 diff --git a/tests/php/VersionedTest.php b/tests/php/VersionedTest.php index c6976e9c..ef8d2aa4 100644 --- a/tests/php/VersionedTest.php +++ b/tests/php/VersionedTest.php @@ -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');