Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't run queries when trying to retrieve versions for unsaved records #352

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those tests passed with the old logic.

$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