FIX 7934 When lazy loading fields respect version of the record

This commit is contained in:
jean 2012-10-10 14:59:45 +13:00 committed by Ingo Schommer
parent 68894c1e35
commit fbfff8df28
3 changed files with 58 additions and 5 deletions

View File

@ -2056,7 +2056,11 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
// 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
@ -2069,7 +2073,8 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
}
if ($columns) {
$query = $dataQuery->query(); // eh?
$query = $dataQuery->query();
$this->extend('augmentLoadLazyFields', $query, $dataQuery, $this->record);
$this->extend('augmentSQL', $query, $dataQuery);
$dataQuery->setQueriedColumns($columns);

View File

@ -257,6 +257,21 @@ class Versioned extends DataExtension {
. $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

View File

@ -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',
@ -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() {
@ -244,4 +249,32 @@ class DataObjectLazyLoadingTest extends SapphireTest {
$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();
}
}