Skip to content

Commit

Permalink
FIX 7934 When lazy loading fields respect version of the record
Browse files Browse the repository at this point in the history
  • Loading branch information
patbolo authored and chillu committed Feb 14, 2013
1 parent b25b6d4 commit e2bf964
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
9 changes: 7 additions & 2 deletions model/DataObject.php
Expand Up @@ -2075,7 +2075,11 @@ protected function loadLazyFields($tableClass = null) {
// TableField sets the record ID to "new" on new row data, so don't try doing anything in that case
if(!is_numeric($this->record['ID'])) return false;

$dataQuery->where("\"$tableClass\".\"ID\" = {$this->record['ID']}")->limit(1);
// Limit query to the current record, unless it has the Versioned extension,
// in which case it requires special handling through augmentLoadLazyFields()
if (!isset($this->record['Version'])){
$dataQuery->where("\"$tableClass\".\"ID\" = {$this->record['ID']}")->limit(1);
}
$columns = array();

// Add SQL for fields, both simple & multi-value
Expand All @@ -2088,7 +2092,8 @@ protected function loadLazyFields($tableClass = null) {
}

if ($columns) {
$query = $dataQuery->query(); // eh?
$query = $dataQuery->query();
$this->extend('augmentLoadLazyFields', $query, $dataQuery, $this->record);
$this->extend('augmentSQL', $query, $dataQuery);

$dataQuery->setQueriedColumns($columns);
Expand Down
15 changes: 15 additions & 0 deletions model/Versioned.php
Expand Up @@ -257,6 +257,21 @@ public function augmentSQL(SQLQuery &$query, DataQuery &$dataQuery = null) {
. $dataQuery->getQueryParam('Versioned.mode'));
}
}

/**
* For lazy loaded fields requiring extra sql manipulation, ie versioning
* @param SQLQuery $query
* @param DataQuery $dataQuery
* @param array $record
*/
function augmentLoadLazyFields(SQLQuery &$query, DataQuery &$dataQuery = null, $record) {
$dataClass = $dataQuery->dataClass();
if (isset($record['Version'])){
$dataQuery->where("\"$dataClass\".\"RecordID\" = " . $record['ID']);
$dataQuery->where("\"$dataClass\".\"Version\" = " . $record['Version']);
$dataQuery->setQueryParam('Versioned.mode', 'all_versions');
}
}

/**
* Keep track of the archive tables that have been created
Expand Down
39 changes: 36 additions & 3 deletions tests/model/DataObjectLazyLoadingTest.php
Expand Up @@ -6,9 +6,12 @@

class DataObjectLazyLoadingTest extends SapphireTest {

static $fixture_file = 'DataObjectTest.yml';
static $fixture_file = array(
'DataObjectTest.yml',
'VersionedTest.yml'
);

// These are all defined in DataObjectTest.php
// These are all defined in DataObjectTest.php and VersionedTest.php
protected $extraDataObjects = array(
'DataObjectTest_Team',
'DataObjectTest_Fixture',
Expand All @@ -18,7 +21,9 @@ class DataObjectLazyLoadingTest extends SapphireTest {
'DataObjectTest_FieldlessSubTable',
'DataObjectTest_ValidatedObject',
'DataObjectTest_Player',
'DataObjectTest_TeamComment'
'DataObjectTest_TeamComment',
'VersionedTest_DataObject',
'VersionedTest_Subclass'
);

public function testQueriedColumnsID() {
Expand Down Expand Up @@ -244,4 +249,32 @@ public function testLazyLoadedFieldsGetAllFields() {
$this->assertArrayNotHasKey('SubclassDatabaseField_Lazy', $subteam1Lazy->toMap());
$this->assertArrayHasKey('SubclassDatabaseField', $subteam1Lazy->toMap());
}

public function testLazyLoadedFieldsOnVersionedRecords() {
// Save another record, sanity check that we're getting the right one
$obj2 = new VersionedTest_Subclass();
$obj2->Name = "test2";
$obj2->ExtraField = "foo2";
$obj2->write();

// Save the actual inspected record
$obj1 = new VersionedTest_Subclass();
$obj1->Name = "test";
$obj1->ExtraField = "foo";
$obj1->write();
$version1 = $obj1->Version;
$obj1->Name = "test2";
$obj1->ExtraField = "baz";
$obj1->write();
$version2 = $obj1->Version;

$reloaded = Versioned::get_version('VersionedTest_Subclass', $obj1->ID, $version1);
$this->assertEquals($reloaded->Name, 'test');
$this->assertEquals($reloaded->ExtraField, 'foo');

$reloaded = Versioned::get_version('VersionedTest_Subclass', $obj1->ID, $version2);
$this->assertEquals($reloaded->Name, 'test2');
$this->assertEquals($reloaded->ExtraField, 'baz');
$obj1->delete();
}
}

0 comments on commit e2bf964

Please sign in to comment.